gpt4 book ai didi

iphone - 从 UITableView 中删除单元格 - 错误

转载 作者:行者123 更新时间:2023-11-28 23:15:22 24 4
gpt4 key购买 nike

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

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

请注意,tableArrays 是一个类变量(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];

}

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

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 in

[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]);

关于iphone - 从 UITableView 中删除单元格 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6695076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com