用animation重新加载表格视图单元格(Swift)

有没有办法重新加载具有animation多个部分的特定UITableView单元格?

我一直在使用:

 self.TheTableView.reloadSections(NSIndexSet(index: 1), withRowAnimation: UITableViewRowAnimation.Right) 

这虽然激活了该部分中的所有单元格。 另一种select是使用:

 self.TheTableView.reloadRowsAtIndexPaths(<#indexPaths: [AnyObject]#>, withRowAnimation: <#UITableViewRowAnimation#>) 

编辑:

使用reloadRowsAtIndexPaths后

由于未捕获的exception“NSInternalInconsistencyException”而终止应用程序,原因是:“无效的更新:第1部分中的行数无效。更新(5)后现有节中包含的行数必须等于该节中包含的行数(4)加上或减去从该部分插入或删除的行数(0插入,0删除),加上或减去移入或移出该部分的行数(0移入,0移入出)。”

尝试通过将对象附加到数组中来find重新加载tableview的顺利方式。

在reloadRowsAtIndexPaths工作之前调用TheTableView.reloadData(),但animation是有问题的。 还有另一种方法吗?

为什么不直接重新加载单元格? indexPath包含部分和行,因此只需跟踪要重新加载的部分并填充该数组中的索引即可。

 var indexPath1 = NSIndexPath(forRow: 1, inSection: 1) var indexPath2 = NSIndexPath(forRow: 1, inSection: 2) self.tableView.reloadRowsAtIndexPaths([indexPath1, indexPath2], withRowAnimation: UITableViewRowAnimation.Automatic) 

根据你的评论,你正在寻找改变你的数组,并有变化tableViewanimation。 如果是这样的话,你应该考虑使用UITableViews的beginUpdates()和endUpdates(),甚至是NSFetchedResultsController,这样它就可以干净地处理所有的更新animation。

 self.tableView.beginUpdates() // Insert or delete rows self.tableView.endUpdates() 

如果你使用Core Data,我build议使用NSFetchedResultsController ,因为它简化了整个过程。 否则,你必须自己处理更改。 换句话说,如果你的数组发生了变化,你需要使用beginUpdates()和endUpdates()手动删除或插入tableview中的行。 如果您不使用核心数据,请研究这个以了解它是如何处理的。

在Swift 3.0中

我们可以在tableview中重新加载特定部分的特定行

 let indexPath = IndexPath(item: 2, section: 0) self.tableView.reloadRows(at: [indexPath], with: .automatic) 

如果你想在swift 3.0中重新加载单个部分,你可以这样做:

 tableView.reloadSections(IndexSet(integer: 0), with: .automatic) 

在官方文档中定义IndexSet

pipe理一Set整数值,通常在Cocoa API中用作索引types。 有效整数值的范围在(0,INT_MAX-1)中。 超出此范围的任何内容都是错误的。

所以IndexSet被设置为关于索引,只是将一些Int值放在[]中

 // for reload one section tableView.reloadSections([section], with: .automatic) // or using like this tableView.reloadSections(IndexSet(integer: section), with: .automatic) // for reload multi sections tableView.reloadSections([1, 2, 3, section], with: .automatic)