【问题标题】:NSFetchedResultsController can't scroll while table updating表更新时 NSFetchedResultsController 无法滚动
【发布时间】:2016-03-01 14:43:48
【问题描述】:

我有一个 tableView 和 NSFetchedResultsController 来监听项目更新。

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 {
                    self.tableView.insertRowsAtIndexPaths([_newIndexPath], withRowAnimation: .None)
                }
            case .Delete:
                if let _indexPath = indexPath {
                    self.tableView.deleteRowsAtIndexPaths([_indexPath], withRowAnimation: .None)
                }
            case .Update:
                if let _indexPath = indexPath {
                    self.tableView.reloadRowsAtIndexPaths([_indexPath], withRowAnimation: .None)
                }
            default:
                self.tableView.reloadData()
        }

        self.items = controller.fetchedObjects as! [Item]
    }

func controllerDidChangeContent(controller: NSFetchedResultsController) {
        self.tableView.endUpdates()
    }

当用户添加项目时,我每 1-2 秒在数据库中更新一次,我无法向下滚动,因为控制器刷新项目并将我滚动到顶部。

【问题讨论】:

  • 您是否也实现了controllerWillChangeContent 和controllerDidChangeContent?
  • 是的,我更新了答案
  • 所以你不能向下滚动,因为你的 tableView 正在重新加载并带你回到顶部?
  • 我打印了结果:更新,每次都调用方法...所以应该只更新一行
  • 只是好奇.. 如果您要插入和删除行,为什么没有 rowAnimation?摆脱self.tableView.reloadData() 看看是否能解决您的问题...您不需要重新加载整个tableView 数据,因为您已经使用insertRowsAtIndexPaths、deleteRowsAtIndexPaths 和reloadRowsAtIndexPaths 方法更新它...那是多余的,可能是您的问题的原因

标签: ios swift uitableview


【解决方案1】:

我发现了问题。这是因为我使用:

self.tableView.rowHeight = UITableViewAutomaticDimension

感谢您的回答:UITableView bouncing back to the top of a section when calling reloadRowsAtIndexPaths

【讨论】:

    【解决方案2】:

    您是否尝试过删除默认大小写?每次项目移动时,它都会重新加载您的表格视图。

    尝试像这篇文章中描述的那样实现 .Move 案例:http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller。

    【讨论】:

    • 可能需要检查单元格是否可见?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多