Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UICollectionView中保持垂直滚动位置_Ios_Swift_Core Data_Uicollectionview_Nsfetchedresultscontroller - Fatal编程技术网

Ios 如何在UICollectionView中保持垂直滚动位置

Ios 如何在UICollectionView中保持垂直滚动位置,ios,swift,core-data,uicollectionview,nsfetchedresultscontroller,Ios,Swift,Core Data,Uicollectionview,Nsfetchedresultscontroller,我已经尝试了堆栈溢出的所有答案,但没有一个对我有用 我有一个加载更多消息按钮,可以加载以前的消息。我希望能够保持垂直位置 var blockOperations = [NSBlockOperation]() func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType t

我已经尝试了堆栈溢出的所有答案,但没有一个对我有用

我有一个加载更多消息按钮,可以加载以前的消息。我希望能够保持垂直位置

var blockOperations = [NSBlockOperation]()

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    if type == .Insert {
       blockOperations.append(NSBlockOperation(block: {
            self.collectionView?.insertItemsAtIndexPaths([newIndexPath!])
        }))
    }
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
    collectionView?.performBatchUpdates({

        for operation in self.blockOperations {
            operation.start()
        }

    }, completion: { (completed) in

       let lastItem = self.fetchedResultsControler.sections![0].numberOfObjects - 1
       let indexPath = NSIndexPath(forItem: lastItem, inSection: 0)
       //self.collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)

    })

}
有没有一个有效的方法来做到这一点