ios - 表更新时 NSFetchedResultsController 无法滚动

标签 ios swift uitableview

我有一个 tableViewNSFetchedResultsController 来监听项目更新。

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 秒在数据库中更新一次,但我无法向下滚动,因为 Controller 刷新项目并将我滚动到顶部。

最佳答案

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

self.tableView.rowHeight = UITableViewAutomaticDimension

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

关于ios - 表更新时 NSFetchedResultsController 无法滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35725884/

相关文章:

ios - 以编程方式检查是否设置了密码锁

ios - 如何在此 View Controller 中显示 inputAccessoryView

objective-c - 在 XCTest 中完成测试套件运行后执行操作

ios - 如何在 iOS 中使用异步方法(JSON Restful 服务)?

ios - 与 CGContextDrawImage 相比,为什么 UIImageView 如此占用内存

ios - 使用 AVPlayerViewController 从 HTTP 实时流中获取视频详细信息

ios - 由于 heightAnchor,无法垂直滚动 collectionview

iphone - tableView 的 commitEditingStyle 并知道何时点击 'Done' 按钮

ios - 快速将 uiimageview 保存到 tableview?

ios - 获取 TableView 的 CustomCell 中的 UIViewController