我正在尝试从选定的表行创建一个数组,以便可以将其推送到新视图。我的问题是从数组中删除未选择的行,尽管我选择了其他项目,但它会引发超出范围的索引错误。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//listOfItems is a Pre Populated Array for the table
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
//the array i want my selected items to be added to
NSArray *array = names;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[names addObject:cellValue];
NSLog(@"ARRAY: %@", array);
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[names removeObjectAtIndex:indexPath.row];
NSLog(@"ARRAY: %@", array);
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
}
如何在正在创建的数组中找到索引号,以便删除正确的值?任何帮助将不胜感激:-) 谢谢!
-----解决方案----- 这是另一种方法,以防其他人想知道。
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *selected = [listOfItems objectAtIndex:indexPath.row];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[names addObject:selected];
NSLog(@"ARRAY ADDED %@", names);
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[names removeObject:selected];
NSLog(@"ARRAY DELETED %@", names);
}