我想弄清楚如何从这个NSMutableArray中删除一个项目用于分区表格。这是我如何建立数据源:从特定键的NSMutableArray中删除项目
listOfItems = [[NSMutableArray alloc] init];
NSArray *appleComputers = [NSArray arrayWithObjects:@"iPhone",@"iPod",@"MacBook",@"MacBook Pro",nil];
NSDictionary *appleComputersDict = [NSDictionary dictionaryWithObject:appleComputers forKey:@"Computers"];
NSArray *otherComputers = [NSArray arrayWithObjects:@"HP", @"Dell", @"Windows", nil];
NSDictionary *otherComputersDict = [NSDictionary dictionaryWithObject:otherComputers forKey:@"Computers"];
[listOfItems addObject:appleComputersDict];
[listOfItems addObject:otherComputersDict];
我想弄清楚如何删除,从这个数组说“iPod”?我尝试了很多东西,其中包括下面的东西,但没有成功。
[listOfItems removeObjectAtIndex:indexPath.row];
(这里的问题是,没有为实际的部分没有提及...)
谢谢!
UPDATE:这是我删除该行:
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSMutableArray * section = [listOfItems objectAtIndex:indexPath.section];
[section removeObjectAtIndex:indexPath.row];
[tblSimpleTable reloadData];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// some code...
}
}
这是一个很好的开始,但是这个解决方案并没有更新我的原始数据源,而是它只创建了一个没有删除项目的新数组。 – TommyG
它根本不创建新的数组。它会更新您最初添加到列表中的数组。如果您有一个“原始数据源”,而不是您在示例代码中使用静态项目说明的“原始数据源”,那么您可能应该澄清一下您的问题。 – Steve
我不知道。 listOfItems是唯一的一个。但是在你的代码中,你是从listOfItems中创建“section”,然后从“section”中删除一个对象,它是listOfItems的一个子集,而不再是它的一部分。所以listOfItems没有被更新。 – TommyG