有多个select的UITableview?

我正在UITableviewCell中的多个select,我可以这样做,问题发生时,选定的值被添加到数组中,并将其从数组中删除。 我已经从另一个数组中select了值。我认为,下面的场景我犯了错误:“我已经select了多行,然后我一个接一个地取消了它的崩溃”,以下是崩溃– [__ NSArrayM removeObjectAtIndex:]:index 1 beyond bounds [0..0]'

我可以理解的问题,但无法解决,有无论如何获得选定的数组值?

- (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; } 

试试这个,在你的委托方法中临时引用并检查

 - (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; } 

错误说[__NSArrayM removeObjectAtIndex:]:索引1超越界限[0..0]' ,您尝试删除范围内的索引。您的数组只包含一个值,但您尝试删除第二个索引。

喜欢

为CellforRowAtIndex中的复选标记值创build一个公共数组

 { 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]; }