简体   繁体   English

当使用2个相同的NSIndexPath调用NSFetchedResultsChangeMove时,如何保持单元格处于选中状态?

[英]How to keep a cell selected when NSFetchedResultsChangeMove is called with 2 identical NSIndexPath?

If I click on a cell and a background Core Data refresh happens, my cell gets deselected. 如果我单击一个单元格,然后发生后台核心数据刷新,则取消选中我的单元格。

After investigation, my NSFetchedResultsControllerDelegate 's implementation of controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: gets called with a type of NSFetchedResultsChangeMove but indexPath and newIndexPath are identical (ie same row/section values). 经过调查,我NSFetchedResultsControllerDelegate的执行controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:被称为有型的NSFetchedResultsChangeMoveindexPathnewIndexPath完全相同(即同一行/段值)。

The NSFetchedResultsChangeUpdate type doesn't get called. NSFetchedResultsChangeUpdate类型不会被调用。

My implementation of controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: is the typical one provided in Apple's documentation. 我的controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:是Apple文档中提供的典型实现。 I believe that switching from: 我相信从:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {

    ...

    switch(type) {

        ...

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

to: 至:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {

    ...

    switch(type) {

        ...

        case NSFetchedResultsChangeMove:
            if (indexPath.section != newIndexPath.section || indexPath.row != newIndexPath.row) {
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
            break;
    }
}

would solve my problem. 会解决我的问题。

My question is: is this fix correct or do I miss something that could explain why my update is of the NSFetchedResultsChangeMove type? 我的问题是:此修复程序正确吗?或者我错过了什么可以解释为什么我的更新是NSFetchedResultsChangeMove类型的事情吗?

My problem was that I was that I was assigning values to NSManagedObject instances that were the same as before. 我的问题是我正在为与以前相同的NSManagedObject实例分配值。 ie, if foo.bar was 2, I would call the API and call a new foo.bar = 2 assignment on my object. 即,如果foo.bar为2,我将调用API并在对象上调用新的foo.bar = 2分配。 Even though, values were identical, Core Data was still considering the value had changed. 即使值是相同的,Core Data仍在考虑值已更改。

It is still weird that I got a NSFetchedResultsChangeMove type instead of a NSFetchedResultsChangeUpdate type but heh. 我还是得到了NSFetchedResultsChangeMove类型而不是NSFetchedResultsChangeUpdate类型,但是还是很奇怪。

What I did to resolve this is: 为了解决这个问题,我要做的是:

if ([object.changedValues count] == 0) {
    // Reverting changed on object
    [context refreshObject:object mergeChanges:NO]
}

At the end of each update, rather than comparing attributes one by one. 在每次更新结束时,而不是一个一个地比较属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保持UITableView单元格处于选中状态 - How to keep a UITableView cell selected 如何跟踪使用NSIndexPath选择的单元格? - How do I keep track of cells that have been selected using NSIndexPath? 如何在调用另一个动作时保持用户选择的图像显示 - how to keep a user selected image displayed when another action is called 如何将单元格的值/字符串内容转换为nsindexpath - How to convert value / string content of cell to nsindexpath 如何在UITableView中移动没有NSIndexPath的单元格? - How to move cell without NSIndexPath in UITableView? 什么时候被称为:-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - when is this called : - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 当重用单元格时,如何将UISegmentedControl的选定段保留在UITableView中? - How do I keep the selected segment of UISegmentedControl inside UITableView when cell is reused? 在tableview单元格上调用setSelected时,为什么我的选择状态无法保留? - Why is my selected state not preserved when called setSelected on tableview cell? 如何在滚动 tableView 时保持选中单元格? - How can I keep a cell selected while scrolling the tableView? 编辑该单元格时如何保持UITableViewCell处于选中状态? - How to keep a UITableViewCell selected while I edit that cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM