嗯,基本上你想做的是:
- 从数据源(数组)中删除行。
- 告诉表视图您已从数据源中删除了一行。
正确的代码应该是这样的:
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableFavoritesData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
编辑:我没有注意到另一个错误。
您需要指定动画的类型,而不仅仅是传递 YES 或 NO。例如:UITableViewRowAnimationFade。查看可能的 UITableViewRowAnimation 值here。
编辑 2:对于下面的评论(评论格式很烂):
查看文档中的NSNotificationCenter,尤其是 addObserver:selector:name:object: 和 postNotificationName:object: 方法。
在您的其他视图控制器中(可能是 viewDidLoad 方法):
[[NSNotificationServer defaultCenter] addObserver:self selector:@selector(deletedRow:) name:@"RowDeleted" object:nil];
-(void) deletedRow:(NSNotification*) notification
{
NSDictionary* userInfo = [notification userInfo];
NSIndexPath indexPath = [userInfo objectForKey:@"IndexPath"];
// your code here
}
在删除行时:
if (editingStyle == UITableViewCellEditingStyleDelete) {
...
[[NSNotificationServer defaultCenter] postNotificationName:@"RowDeleted" object:self userInfo:[NSDictionary dictionaryWithObject:indexPath forKey:@"IndexPath"]];
}
请记住,当您释放另一个 UIViewController 时,您需要从通知中心移除观察者:
[[NSNotificationServer defaultCenter] removeObserver: self];
希望我没有犯太多错误,我无法访问 XCode atm。