【发布时间】:2013-05-23 12:32:07
【问题描述】:
从表视图中逻辑删除对象时遇到问题。想法是如果不应该显示项目,则将标志设置为“已删除”之类的项目为 1。加载数据谓词不应加载此类行。
带有谓词和创建 NSFetchedResultController 的代码:
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Photo"];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"section" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]];
request.predicate = [NSPredicate predicateWithFormat:@"tag = %@ and deleted == %@", self.tag, @(0)];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.tag.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
在滑动时删除。这是滑动的操作:
- (IBAction)deletePhoto:(UISwipeGestureRecognizer *)sender {
CGPoint location = [sender locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
if (indexPath) {
Photo *photo = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.tag.managedObjectContext performBlock:^{
photo.deleted = @(1); //This must trigger data change and view must be redrawn. But it's not happending for some reason.
}];
}
}
这里的问题是该行仅在应用程序重新启动后才会被删除。由于某种原因,FetchedResultsController 不会从数据中删除更改的值,并且表格视图仍然显示该行。
我尝试从表视图中显式删除该项目,但在第 0 节中的项目数量不正确时出现异常(这就是我假设该行仍在获取结果中的原因)。显式调用 [self.tableView reloadData] 或 [self performFetch] 不会重新加载数据,并且项目仍然存在。
我几乎想不出应该怎么做才能重新加载数据。
提前致谢。
【问题讨论】:
标签: uitableview reloaddata delete-row nsfetchedresultscontroller