UISearchDisplayController动画reloadData

时间:2013-02-10 11:27:30

标签: uitableview core-data uisearchdisplaycontroller nsfetchedresultscontroller

我一直在阅读有关UISearchDisplayController及其delegate的所有文档,但在搜索条件发生变化时,我找不到任何动画表格视图的方法。

我正在使用这两种方法: UISearchDisplayController methods

他们都返回YES但仍无法找到任何方法来执行类似的操作: UITableView Methods

我不知道这是否重要,但我使用NSfetchedResultsController填充UITableView

中的UISearchDisplayController

非常感谢!

2 个答案:

答案 0 :(得分:8)

当搜索字符串或范围发生更改时,您为获取的结果控制器分配新的提取请求,因此必须调用performFetch以获取新的结果集。 performFetch重置控制器的状态,它不会触发任何FRC委托方法。

因此,在更改获取请求后,必须“手动”更新表视图。在大多数示例程序中,这是通过

完成的
  • 在搜索表视图上调用reloadData,或
  • YESshouldReloadTableForSearchString返回shouldReloadTableForSearchScope

效果相同:重新加载搜索表视图而不动画。

我认为在搜索谓词更改时,没有任何内置/简单方法可以为表视图更新设置动画。但是,您可以尝试以下(这只是一个想法,我实际上并没有尝试过这个):

  • 在更改获取请求之前,请复制旧结果集:

    NSArray *oldList = [[fetchedResultsController fetchedObjects] copy];
    
  • 将新的获取请求分配给FRC并致电performFetch

  • 获取新的结果集:

    NSArray *newList = [fetchedResultsController fetchedObjects];
    
  • 请勿在搜索表视图上调用reloadData

  • 比较oldListnewList以检测新的和已删除的对象。为新对象调用insertRowsAtIndexPaths,为删除的对象调用deleteRowsAtIndexPaths。这可以通过并行遍历两个列表来完成。
  • NOshouldReloadTableForSearchString返回shouldReloadTableForSearchScope

正如我所说,这只是一个想法,但我认为它可行。

答案 1 :(得分:1)

我知道这个问题已经过时了,但我最近遇到了这个问题,想要一个无论NSFetchedResultsController如何初始化都会有效的解决方案。它基于@ martin-r的上述答案。

这里有相应的要点:https://gist.github.com/stephanecopin/4ad7ed723f9857d96a777d0e7b45d676

import CoreData

extension NSFetchedResultsController {
    var predicate: NSPredicate? {
        get {
            return self.fetchRequest.predicate
        }
        set {
            try! self.setPredicate(newValue)
        }
    }

    var sortDescriptors: [NSSortDescriptor]? {
        get {
            return self.fetchRequest.sortDescriptors
        }
        set {
            try! self.setSortDescriptors(newValue)
        }
    }

    func setPredicate(predicate: NSPredicate?) throws {
        try self.setPredicate(predicate, sortDescriptors: self.sortDescriptors)
    }

    func setSortDescriptors(sortDescriptors: [NSSortDescriptor]?) throws {
        try self.setPredicate(self.predicate, sortDescriptors: sortDescriptors)
    }

    func setPredicate(predicate: NSPredicate?, sortDescriptors: [NSSortDescriptor]?) throws {
        func updateProperties() throws {
            if let cacheName = cacheName {
                NSFetchedResultsController.deleteCacheWithName(cacheName)
            }

            self.fetchRequest.predicate = predicate
            self.fetchRequest.sortDescriptors = sortDescriptors
            try self.performFetch()
        }

        guard let delegate = self.delegate else {
            try updateProperties()
            return
        }

        let previousSections = self.sections ?? []
        let previousSectionsCount = previousSections.count
        var previousObjects = Set(self.fetchedObjects as? [NSManagedObject] ?? [])
        var previousIndexPaths: [NSManagedObject: NSIndexPath] = [:]
        previousObjects.forEach {
            previousIndexPaths[$0] = self.indexPathForObject($0)
        }
        try updateProperties()
        let newSections = self.sections ?? []
        let newSectionsCount = newSections.count
        var newObjects = Set(self.fetchedObjects as? [NSManagedObject] ?? [])
        var newIndexPaths: [NSManagedObject: NSIndexPath] = [:]
        newObjects.forEach {
            newIndexPaths[$0] = self.indexPathForObject($0)
        }

        let updatedObjects = newObjects.intersect(previousObjects)
        previousObjects.subtractInPlace(updatedObjects)
        newObjects.subtractInPlace(updatedObjects)

        var moves: [(object: NSManagedObject, fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath)] = []
        updatedObjects.forEach { updatedObject in
            if let previousIndexPath = previousIndexPaths[updatedObject],
                let newIndexPath = newIndexPaths[updatedObject]
            {
                if previousIndexPath != newIndexPath {
                    moves.append((updatedObject, previousIndexPath, newIndexPath))
                }
            }
        }

        if moves.isEmpty && previousObjects.isEmpty && newObjects.isEmpty {
            // Nothing really changed
            return
        }

        delegate.controllerWillChangeContent?(self)

        moves.forEach {
            delegate.controller?(self, didChangeObject: $0.object, atIndexPath: $0.fromIndexPath, forChangeType: .Move, newIndexPath: $0.toIndexPath)
        }

        let sectionDifference = newSectionsCount - previousSectionsCount
        if sectionDifference < 0 {
            (newSectionsCount..<previousSectionsCount).forEach {
                delegate.controller?(self, didChangeSection: previousSections[$0], atIndex: $0, forChangeType: .Delete)
            }
        } else if sectionDifference > 0 {
            (previousSectionsCount..<newSectionsCount).forEach {
                delegate.controller?(self, didChangeSection: newSections[$0], atIndex: $0, forChangeType: .Insert)
            }
        }

        previousObjects.forEach {
            delegate.controller?(self, didChangeObject: $0, atIndexPath: previousIndexPaths[$0], forChangeType: .Delete, newIndexPath: nil)
        }
        newObjects.forEach {
            delegate.controller?(self, didChangeObject: $0, atIndexPath: nil, forChangeType: .Insert, newIndexPath: newIndexPaths[$0])
        }

        delegate.controllerDidChangeContent?(self)
    }
}

它在NSFetchedResultsControllerpredicatesortDescriptors上公开了2个属性,这些属性与fetchRequest中的内容相同。
当设置这些属性中的任何一个时,控制器将自动计算更改的更改并通过委托发送它们,因此希望没有重大的代码更改。 如果您不想要动画,仍然可以直接在predicate上设置sortDescriptorsfetchRequest