当NSFetchedResultsController添加新记录时,如何阻止向上滚动UITableView? [英] How to prevent from scrolling UITableView up when NSFetchedResultsController add new record?

查看:114
本文介绍了当NSFetchedResultsController添加新记录时,如何阻止向上滚动UITableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 UITableView 就会向上滚动。 NSFetchedResultsController知道它,将此注释放在表的底部,并将表滚动到顶部。这是正常的吗?

My UITableView is scrolling up once I add a new comment to CoreData. NSFetchedResultsController knows about it, puts this comment at the bottom of the table, and scroll table to top. Is it normal?

我的例子:

在我添加评论之前:

在我添加评论后(不是预期的行为):

Just after I add a comment (not expected behavior):

它应该是这样的(在我手动手动滑动后):

这可能与以下情​​况有关:

This may be connected with following situation:


  1. 这是我的的排序描述符NSFetchedResultsController

NSSortDescriptor(key: "createdAt", ascending: false) //from the latest to the oldest


  • <但是我需要显示我对反向索引路径的注释(最新版本位于最底层)。换句话说,当我需要使用 indexPath 时,我使用:

  • But I need to display my comments for reversed index paths (the latest are at the very bottom). In other words, everywhere when I need to use indexPath I use:

    private func reversedIndexPathForIndexPath(indexPath: NSIndexPath) -> NSIndexPath {
        return NSIndexPath(forRow: fetchedResultsController.fetchedObjects!.count - indexPath.row - 1, inSection: 0)
    }
    


  • NSFetchedResultsControllerDelegate

    //MARK: - NSFetchedResultsControllerDelegate
    
    func controllerWillChangeContent(controller: NSFetchedResultsController) {
        self.tableView.beginUpdates()
    }
    
    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    
        switch type {
        case .Insert:
            if let newIndexPath = newIndexPath {
                tableView.insertRowsAtIndexPaths([reversedIndexPathForIndexPath(newIndexPath)], withRowAnimation: .Fade)
            }
        case .Delete:
            if let indexPath = indexPath {
                tableView.deleteRowsAtIndexPaths([reversedIndexPathForIndexPath(indexPath)], withRowAnimation: .Fade)
            }
        case .Update:
            if let indexPath = indexPath {
                tableView.reloadRowsAtIndexPaths([reversedIndexPathForIndexPath(indexPath)], withRowAnimation: .Fade)
            }
        case .Move:
            if let indexPath = indexPath, let newIndexPath = newIndexPath {
                tableView.deleteRowsAtIndexPaths([reversedIndexPathForIndexPath(indexPath)], withRowAnimation: .Fade)
                tableView.insertRowsAtIndexPaths([reversedIndexPathForIndexPath(newIndexPath)], withRowAnimation: .Fade)
            }
        }
    }
    
    func controllerDidChangeContent(controller: NSFetchedResultsController) {
        tableView.endUpdates()
    }
    

    是它与我的问题有关吗?

    Is it connected to my problem?

    推荐答案

    编辑答案:

    我能够重现这个故障执行以下步骤:

    I was able to reproduce this glitch with the following steps:


    1. UITableView 及其 rowHeight 设置为 UITableViewAutomaticDimension 且非零 estimatedRowHeight

    2. 表格滚动到最底部,最后一个单元格在底部边缘完全可见。

    3. 在最后一个单元格下面插入新单元格会导致单元格的视觉移动几点down(在我的设置中它从5到40不等)。

    1. UITableView with its rowHeight set to UITableViewAutomaticDimension and non-zero estimatedRowHeight.
    2. Table scrolled to its very bottom, with the last cell fully visible at the bottom margin.
    3. Inserting the new cell below the last one results in visual shift of the cells a few points down (in my setup it differs from 5 to 40).

    此行为的来源需要进一步调查。

    The source of this behavior needs additional investigation.

    目前唯一的解决方案是不使用 UITableViewAutomaticDimension 并从 tableView返回行高:heightForRowAtIndexPath:

    For now the only solution is not to use UITableViewAutomaticDimension and return row heights from tableView:heightForRowAtIndexPath:.

    顺便说一下,反向索引路径的实现有一个重大缺陷。当FRC调用它的委托方法 controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:时,所有删除索引路径都属于预更改数据集,所有插入索引路径 - 发布 - 改变一个。当你在 controllerWillChangeContent: controllerDidChangeContent:之后, fetchedResultsController.fetchedObjects 将包含更改后的数据集,我想(但需要进行检查)。

    By the way, implementation of inverted index paths has one significant flaw. When FRC calls it's delegate method controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:, all deletion index paths belongs to pre-change data set, and all insertion index paths - to post-change one. When you are in-beetween controllerWillChangeContent: and controllerDidChangeContent:, fetchedResultsController.fetchedObjects will contain post-change data set, i suppose (needs to be checked, though).

    这篇关于当NSFetchedResultsController添加新记录时,如何阻止向上滚动UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆