case NSFetchedResultsChangeMove:
{
NSLog(@"indexPath row = %d; newIndexPath row = %d", indexPath.row, newIndexPath.row);
NSArray* indexPathArray = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationFade];
[indexPathArray release];
NSArray* newIndexPathArray = [[NSArray alloc] initWithObjects:newIndexPath, nil];
[self.tableView insertRowsAtIndexPaths:newIndexPathArray withRowAnimation:UITableViewRowAnimationFade];
[newIndexPathArray release];
}
break;
上面是标准写法。(和apple的写法有点差别,不过我还是喜欢自己释放内存 不喜欢那个 autorelease)
这个条件是什么时候触发的呢,比如你的 tableView是按照name 使用NSFetchedResultController还获取数据,当修改的时候 name和另外一条重名 或者 排序引起了变化的时候就会触发该条件。 所以我们先删除原有的indexPath 再在newIndexPath插入新的。
可问题是 newIndexPath中的数据是如何被放置进去的呢 从上面的代码中看不出来有放置数据的地方。
原来是 在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
中进行配置数据的。
所以 这个 NSFetchedResultsChangeMove 和NSFetchedResultsChangeUpdate 是完全的不同,所以当你在更新数据的时候 小心 跑到了 MOVE 那里处理了。