objective-c 如何手动删除 UITableView 中的一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/915095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete a row in UITableView manually?
提问by Anh Do
Here's what I've come up with:
这是我想出的:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; // my table view has 2 sections
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
Everytime I build and run, it throws the following exception:
每次我构建和运行时,它都会抛出以下异常:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update must be equal to the number of rows contained in that section before the update, plus or minus the number of rows added or removed from that section.
无效更新:第 0 节中的行数无效。更新后现有节中包含的行数必须等于更新前该节中包含的行数,加上或减去添加或删除的行数那个部分。
It's a bit confusing. The section is set to 1, yet the exception says it's 0.
这有点令人困惑。该部分设置为 1,但异常表示它为 0。
回答by Anh Do
I figured it out.
我想到了。
In addition to the aforementioned code, I also need to make changes to the datasource
除了前面提到的代码,我还需要对数据源进行更改
[items removeObjectAtIndex:0];
回答by Chrizzz
- (IBAction)deleteCustomCellWithUIButton:(id)sender
{
NSLog(@"Message From Custom Cell Received");
NSIndexPath *indexPath = [self.myTableView indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]];
NSUInteger row = [indexPath row];
[self.myDataArray removeObjectAtIndex:row];
[self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

