在UITableViewController
中,我可以使用此实现同时插入行和部分:
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!, atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!) {
if controller == frc {
switch(type) {
case .Insert:
self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
case .Delete:
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
case .Update:
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
default:
break
}
}
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch(type) {
case .Insert:
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case .Delete:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default:
break
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}
UICollectionViewController
怎么样?我还没有找到实施controllerWillChangeContent
和controllerDidChangeContent
的替代方式。
答案 0 :(得分:0)
collectionView
的工作方式与tableView
略有不同,您需要使用performBatchUpdates
var frc: NSFetchedResultsController?
var iip = [NSIndexPath]()
var dip = [NSIndexPath]()
var ins: NSIndexSet?
var des: NSIndexSet?
func controllerWillChangeContent(controller: NSFetchedResultsController) {
}
func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!, atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!) {
if controller == frc {
switch(type) {
case .Insert:
iip.append(newIndexPath)
case .Delete:
dip.append(indexPath)
case .Update:
self.collectionView!.reloadItemsAtIndexPaths([indexPath])
default:
break
}
}
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch(type) {
case .Insert:
ins = NSIndexSet(index: sectionIndex)
case .Delete:
des = NSIndexSet(index: sectionIndex)
default:
break
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.collectionView!.performBatchUpdates({
self.collectionView!.insertItemsAtIndexPaths(self.iip)
self.collectionView!.deleteItemsAtIndexPaths(self.dip)
if self.ins != nil {
self.collectionView!.insertSections(self.ins!)
}
if self.des != nil {
self.collectionView!.deleteSections(self.des!)
}
}, completion: {completed in
self.iip.removeAll(keepCapacity: false)
self.dip.removeAll(keepCapacity: false)
self.ins = nil
self.des = nil
})
}
答案 1 :(得分:0)
我能够通过检查新索引路径的部分是否大于集合视图中的部分数,然后执行批量更新来实现此目的。
let indexPath = IndexPath()
// index path of item to be inserted
if indexPath.section > collectionView.numberOfSections - 1 {
collectionView.performBatchUpdates({
let set = IndexSet(integer: sectionIndex)
collectionView.insertSections(set)
self.collectionView.insertItems(at: [indexPath])
}, completion: nil)
} else {
collectionView.insertItems(at: [indexPath])
}