NSFetchedResultsControllerDelegate NSFetchedResultsChangeMove 是干什么地呢?

本文介绍在Swift中使用UITableView与NSFetchedResultsController时如何处理数据移动情况。当TableView数据因排序改变而发生移动时,需要先删除旧indexPath的数据,然后在新indexPath处插入更新后的数据。文章还解释了cellForRowAtIndexPath方法在这一过程中的作用。
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 那里处理了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值