繁体   English   中英

从 UITableView 中删除单元格 - 错误

[英]Deleting a cell from UITableView - error

在我的 iPhone 应用程序中,我有一个 UITableView 使用以下方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [tableArrays count];
}

请注意, tableArrays是一个 class 变量(NSMutableArrays 的 NSMutableArray - 代表我的 tableView 中每个部分的一个 NSMutableArray)。 现在,那个 UITableView 支持编辑,我有以下方法

- (void)tableView:(UITableView *)theTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    ...some code...

    NSLog(@"tableArrays count is %i",[tableArrays count]);
    [tableArrays removeObjectAtIndex:indexPath.section];
    NSLog(@"tableArrays is now %@",tableArrays);
    NSLog(@"tableArrays count is now %i",[tableArrays count]);


   [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

所以,我运行应用程序,我的表格视图中有两个部分。 行删除工作正常,除非我删除部分中的最后一行。 当我删除第 1 节的最后一行时,根据上面的代码,我看到以下 output:

tableArrays count is 2
tableArrays is now (("Blah1","Blah2"))
tableArrays count is now 1

很明显,我的tableArrays计数减一,但出现以下错误:

...The number of sections contained in the tableView after the update (1) must be qual to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or dleted (0 inserted, 0 deleted).'

我猜在-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 您正在返回对象的总数,并且只想显示一个部分。

尝试在此方法中返回一个。

您还应该在-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;中返回[tableArrays count] .

它应该像这样更好地工作。 还有什么是section

[tableArrays removeObjectAtIndex:section]; ? 你的意思是indexPath.section吗?

感谢您的评论,因此返回节数的方法是正确的,但是您这样做是为了删除一个单元格。

NSLog(@"tableArrays count is %i",[tableArrays count]);
NSMutableArray *sectionArray = [tableArrays objectAtIndex:indexPath.section];
[sectionArray removeObjectAtIndex:indexPath.row];
//[tableArrays removeObjectAtIndex:indexPath.section];
NSLog(@"tableArrays is now %@",tableArrays);
NSLog(@"tableArrays count is now %i",[tableArrays count]);

关键是用

[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];

当您尝试删除部分中的最后一行时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM