使用NSFetchedResultsController自定义UICollectionViewLayout

时间:2016-01-04 08:46:13

标签: ios swift core-data nsfetchedresultscontroller flowlayout

我的收藏视图更新需要帮助, 我有一个自定义布局的集合视图,我从ray wenderlich tutorial和NSFetchedResultsController

实现

这是我更新集合视图的代码:
但该集合不会显示新的更新项目(正常布局都可以正常工作)

func controllerWillChangeContent(controller: NSFetchedResultsController) {

    blockOperations = []
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {

    weak var collection = self.collectionView

    switch type {

    case .Insert:

        guard indexPath != newIndexPath else { return }

        blockOperations.append(

            NSBlockOperation(block: { [weak collection] in

                collection?.insertItemsAtIndexPaths([newIndexPath!])
                })
        )

    case .Update:

        guard indexPath != nil else { return }

        blockOperations.append(

            NSBlockOperation(block: { [weak collection] in

                collection?.reloadItemsAtIndexPaths([indexPath!])
            })
        )

    case .Move:

        guard newIndexPath != nil else { return }

        blockOperations.append(

            NSBlockOperation(block: { [weak collection] in

                collection?.moveItemAtIndexPath(indexPath!, toIndexPath: newIndexPath!)
            })
        )

    case .Delete:

        guard indexPath != nil else { return }

        blockOperations.append(

            NSBlockOperation(block: { [weak collection] in

                collection?.deleteItemsAtIndexPaths([indexPath!])
            })
        )
    }
}

func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {

    weak var collection = self.collectionView

    switch type {

    case .Insert:

        blockOperations.append(

            NSBlockOperation(block: { [weak collection] in

                collection?.insertSections(NSIndexSet(index: sectionIndex))
            })
        )

    case .Update:

        blockOperations.append(

            NSBlockOperation(block: { [weak collection] in

                collection?.reloadSections(NSIndexSet(index: sectionIndex))
            })
        )

    case .Delete:

        blockOperations.append(

            NSBlockOperation(block: { [weak collection] in

                collection?.deleteSections(NSIndexSet(index: sectionIndex))
            })
        )

    case .Move:
        break
    }
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {

    collectionView?.performBatchUpdates({ () -> Void in

        for operation: NSBlockOperation in self.blockOperations {

            operation.start()
        }

        }, completion: { (finished) -> Void in

            if finished{

                self.blockOperations.removeAll(keepCapacity: false)
            }
    })
}

0 个答案:

没有答案