1
我试图从选定的表行创建一个数组,所以我可以将它推送到一个新的视图。我的问题是从数组中删除一个未选中的行,它会引发索引超出范围的错误,尽管我选择了其他项目。表从数组中删除索引路径行值
- (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);
}
我需要数组将值添加到sql语句中。 – Far
@Far:好的...以及我的例子中使用'names'数组似乎是什么问题? – Alladinian
修复了我的问题,谢谢。 – Far