如何在UICollectionView

时间:2016-08-16 19:34:30

标签: ios swift core-data uicollectionview nsfetchedresultscontroller

我已经尝试了Stack Overflow上的所有答案,但没有一个能为我工作。

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

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)

    })

}

有没有一种有效的方法呢?

0 个答案:

没有答案