在阅读了苹果的文档并搜索了谷歌之后,我对此感到有点困惑。问题的要点是,我知道我需要从UITableView和数据源中删除(在本例中是NSMutableArray),但是不管我按照什么顺序执行,它都不会让我这样做。
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[myMutableObjectList removeObjectAtIndex:indexPath.row];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// not used
}
}无论我为这两种情况使用哪种顺序:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[myMutableObjectList removeObjectAtIndex:indexPath.row];它要么抱怨还剩多少个元素,要么说删除没有发生。
删除前的
“(计数)必须在删除后匹配(计数)减去许多您已删除的0插入1删除”
和tableView..。首先,它说计数是相同的,1删除要发生。
和myMutableObjectList..。首先,它说计数是少1(很好!)但是0删除想要发生。
如果我不能这样做,我该怎么办?
更新:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if([myMutableObjectList count] == 0) {
return section;
} else {
return 4; //4 variables in myObject
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"Size of list: %d", [myMutableListList count]);
if([myMutableList count] == 0) {
return 100;
} else {
return [myMutableList count];
}
}发布于 2011-06-24 13:51:55
你的numberOfRowsInSection不符合你想要的行为。它永远不会反映对数据存储的更改。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if([myMutableObjectList count] == 0) {
return section;
} else {
return 4; //4 variables in myObject
}
}你为什么要在这里回来。我想你想要回[myMutableObjectList count]
此方法需要反映所有单元格的当前计数。在执行删除操作时,可以从myMutableObjectList中删除一个对象,但该更改不会影响numberOfRows方法的结果。
https://stackoverflow.com/questions/6458198
复制相似问题