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