简体   繁体   English

从 UITableView 中删除单元格 - 错误

[英]Deleting a cell from UITableView - error

In my iPhone app, I have a UITableView with the following method在我的 iPhone 应用程序中,我有一个 UITableView 使用以下方法

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

Note that tableArrays is a class variable (an NSMutableArray of NSMutableArrays - representing one NSMutableArray for each section in my tableView).请注意, tableArrays是一个 class 变量(NSMutableArrays 的 NSMutableArray - 代表我的 tableView 中每个部分的一个 NSMutableArray)。 Now, that UITableView supports editing, and I have the following method现在,那个 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];

}

So, I run the app and I have two sections in my table view.所以,我运行应用程序,我的表格视图中有两个部分。 Row deletion works fine except when I delete the last row in a section.行删除工作正常,除非我删除部分中的最后一行。 When I delete the last row of section 1, I see the following output, as per the code above:当我删除第 1 节的最后一行时,根据上面的代码,我看到以下 output:

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

So clearly, my tableArrays count decreases by one, but I get the following error:很明显,我的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).'

I guess in -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;我猜在-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; you are returning the total count of objects and you want to display only one section.您正在返回对象的总数,并且只想显示一个部分。

Try returning one in this method.尝试在此方法中返回一个。

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

It should work better like this.它应该像这样更好地工作。 Also what is section in还有什么是section

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

Thank for the comment, so the method that is returning the number of section is correct, but you to do like that to delete a cell.感谢您的评论,因此返回节数的方法是正确的,但是您这样做是为了删除一个单元格。

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

The key is to use关键是用

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

when you are trying to delete the last row in a section.当您尝试删除部分中的最后一行时。

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

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