2015-11-14 2 views
0

Я работаю над несколькими выборами в UITableviewCell, я могу это сделать, проблема возникает, когда выбранное значение добавляется в массив и удаляет его из массива. Я принял выбранное значение из другого массива. Я думаю, следующий сценарий, который я допустил, «я выбрал несколько строк, тогда я отменяю один за другим его сбой» со следующим сбоем: - [__ NSArrayM removeObjectAtIndex:]: index 1 за пределами [0 .. 0] 'UITableview с несколькими вариантами выбора?

Я могу понять проблемы, но не в состоянии решить, есть ли все-таки, чтобы получить выбранные значения в массиве?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; 
    [selectedValue addObject:[[listAns valueForKey:@"o_id"]objectAtIndex:indexPath.row]]; 
    tableViewCell.accessoryView.hidden = NO; 
    tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; 
    [selectedValue removeObjectAtIndex:indexPath.row]; 
    //[selectedValue removeLastObject]; 
    tableViewCell.accessoryView.hidden = YES; 
    tableViewCell.accessoryType = UITableViewCellAccessoryNone; 
} 
+0

Картик, если вы используете ** addObject **, вы должны использовать ** removeObject **, а не ** removeObjectAtIndex **, оба они разные, удалить objectatIndex проверить индекс, объект проверить объект –

+0

@ Anbu.Karthik thanks.both ответ на меня – karthikeyan

+0

Бросьте, что ваше кодирование в порядке, вы сделали ошибку в одной строке, просто измените эту строку ** [selectedValue removeObjectAtIndex: indexPath.row]; ** в ** [selectedValue removeObject: [[listAns valueForKey: @ "o_id"] objectAtIndex: indexPath.row]]; **, то верно работает –

ответ

1

попробовать это, имеет временную ссылку в методе делегата и проверить

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSString *val =[NSString stringWithFormat:@"%@",[[listAns valueForKey:@"o_id"]objectAtIndex:indexPath.row]]; 
    [selectedValue addObject:val]; 
    tableViewCell.accessoryView.hidden = NO; 
    tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSString *val =[NSString stringWithFormat:@"%@",[[listAns valueForKey:@"o_id"]objectAtIndex:indexPath.row]]; 
    [selectedValue removeObject:val]; 
    tableViewCell.accessoryView.hidden = YES; 
    tableViewCell.accessoryType = UITableViewCellAccessoryNone; 
} 
1

ошибка говорит [__NSArrayM removeObjectAtIndex]: индекс 1 за пределами [0 .. 0], вы пытаетесь удалить индекс из range.your массива содержит только одно значение, но вы пытаетесь удалите второй указатель.

сделать как

создать один общий массив для comapre значения галочки в CellforRowAtIndex

{ 

NSMutableArray *storeSelectedcell 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    storeSelectedcell = [NSMutableArray array]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // initilize the cell and additional code 

    if ([storeSelectedcell containsObject:indexPath]) 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 

    else 
    cell.accessoryType = UITableViewCellAccessoryNone; 


    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Remove the selection 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 



    // allow multiple selection 
    if ([storeSelectedcell containsObject:indexPath]) 
     [storeSelectedcell removeObject:indexPath]; 
    else 
     [storeSelectedcell addObject:indexPath]; 

    // finally refresh your table 
    [tableView reloadData]; 
} 
Смежные вопросы