【发布时间】:2016-05-06 11:41:58
【问题描述】:
全屏表格视图仅适用于 iPad 的应用。我已启用滑动以删除我的行。行动画总是在删除(commitEditingStyle 完成)后完成,但有时整个表格视图会冻结。请注意,不是整个 UI,所以它不是阻塞的主线程。我可以点击列标题或点击导航控制器上的后退按钮,但表格本身已锁定且无法滑动。我可以很简单地通过点击我的列标题按钮之一来解冻它。
我完全不知道可能导致冻结的原因。我正在使用 NSFetchedResultsController,这是我的委托代码。这是相当样板(更新:现在不像样板。使用批处理方法):
// MARK: NSFetchedResultsController delegate methods
lazy var deletedSectionIndexes : NSMutableIndexSet = {
return NSMutableIndexSet()
}()
lazy var insertedSectionIndexes : NSMutableIndexSet = {
return NSMutableIndexSet()
}()
lazy var deletedRowIndexPaths : [NSIndexPath] = {
return [NSIndexPath]()
}()
lazy var insertedRowIndexPaths : [NSIndexPath] = {
return [NSIndexPath]()
}()
lazy var updatedRowIndexPaths : [NSIndexPath] = {
return [NSIndexPath]()
}()
func controllerWillChangeContent(controller: NSFetchedResultsController) {
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch(type) {
case .Delete:
if let indexPath = indexPath {
self.deletedRowIndexPaths.appendDistinct(indexPath)
}
case .Update:
if let indexPath = indexPath {
self.updatedRowIndexPaths.appendDistinct(indexPath)
}
case .Insert:
if let newIndexPath = newIndexPath {
self.insertedRowIndexPaths.appendDistinct(newIndexPath)
}
case .Move:
if let indexPath = indexPath, newIndexPath = newIndexPath {
self.insertedRowIndexPaths.appendDistinct(newIndexPath)
self.deletedRowIndexPaths.appendDistinct(indexPath)
}
}
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch(type) {
case .Delete:
self.deletedSectionIndexes.addIndex(sectionIndex)
case .Insert:
self.insertedSectionIndexes.addIndex(sectionIndex)
default:
break
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
self.tableView.insertSections(self.insertedSectionIndexes, withRowAnimation: .None)
self.tableView.deleteSections(self.deletedSectionIndexes, withRowAnimation: .None)
self.tableView.insertRowsAtIndexPaths(self.insertedRowIndexPaths, withRowAnimation: .None)
self.tableView.deleteRowsAtIndexPaths(self.deletedRowIndexPaths, withRowAnimation: .None)
self.tableView.reloadRowsAtIndexPaths(self.updatedRowIndexPaths, withRowAnimation: .None)
self.tableView.endUpdates()
self.insertedSectionIndexes.removeAllIndexes()
self.deletedSectionIndexes.removeAllIndexes()
self.deletedRowIndexPaths.removeAll()
self.insertedRowIndexPaths.removeAll()
self.updatedRowIndexPaths.removeAll()
}
delete 在 didChangeObject 委托方法中被调用,但是,从技术上讲,它并不是真正的删除。我只是将一个属性设置为 -1,然后通过 NSMangagedObjectContext 保存该元素——此时 NSFRC 似乎做了正确的事情,即从使用此谓词获取的获取对象列表中删除它:
NSPredicate(format: "account = %@ and quantity != -1", account)
其中account 是一个有效的帐户管理对象。该行在 90% 或更多的时间没有问题地消失。只是偶尔在动画完成后桌子冻结在我描述的庄园里。它永远不会因为仍然显示删除按钮而冻结,所以我知道它是在调用 commitEditingStyle 之后。删除按钮没有自定义实现。它是滑动删除的默认 UITableView 实现。这是我的 commitEditingStyle 方法:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
if let frameboardItem = self.fetchedResultsController.objectAtIndexPath(indexPath) as? IRMFrameBoardItemMO {
if frameboardItem.isNew {
// If it's never been pushed to the server, just delete locally. This will trigger a table reload
// via NSFetchedResultsController
DataManager.mainContext.deleteObject(frameboardItem)
} else {
// Otherwise mark it with a negative quantity which tells the server to delete it and tells the
// app to hide it.
frameboardItem.quantity = -1
}
do {
try DataManager.mainContext.save()
} catch let error as NSError {
dLog("Something went wrong: \(error.localizedDescription)")
}
}
}
}
您可以在此处观看我所说的视频。两分钟多,所以你可能不想看完整的东西,但我会把它放在这里以供参考。
很想听听任何建议。
更新
我更新了 NSFRC 委托方法以使用批处理方法来确保一次应用所有更新。这并没有解决问题。该表仍会定期冻结。
【问题讨论】:
-
您确定数据源正在正确更新吗?在以下语句中是否存在
frameboardItem是nil的情况:if let frameboardItem = self.fetchedResultsController.objectAtIndexPath(indexPath) as? IRMFrameBoardItemMO?而如果是nil,是不是更新了表而没有更新数据源? -
1) tableView 是否在子视图控制器中? 2)如果出于测试目的,您更改其中一个按钮(确实响应触摸)以触发电视滚动到顶部(或底部),它是否真的滚动?这可能会提供有关电视是否只是忽略触摸或尝试滚动时冻结的线索。
-
@pbasdf 不完全确定我理解你的建议,但是当表格视图冻结时,我可以点击状态栏并且表格视图按预期滚动到顶部。但是,在尝试通过滑动滚动时,它仍然被冻结。
-
@Matt 谢谢 - 所以问题是因为电视没有收到触摸,而不是因为它不能滚动。您的电视或手机是否有任何手势识别器可以吞下触摸?
-
@pbasdf 不。单元格上没有手势识别器。
标签: swift uitableview core-data nsfetchedresultscontroller