使用动画重新加载表视图单元格(Swift) [英] Reload Table view cell with animation (Swift)

查看:119
本文介绍了使用动画重新加载表视图单元格(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to reload specific UITableView cells with multiple sections with animations?

我一直在使用:

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

这会动画中的所有单元格但是。另一种选择是使用:

This animates all the cells in the section though. The other option is to use:

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

编辑:

使用reloadRowsAtIndexPaths后

After using reloadRowsAtIndexPaths

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

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

试图找到一个通过将对象附加到数组中来平滑重新加载tableview的方法。

Trying to find a smooth way of reloading a tableview by appending objects into an array.

调用TheTableView.reloadData ()在reloadRowsAtIndexPaths工作之前,但动画很糟糕。还有另一种方法吗?

Calling TheTableView.reloadData() before reloadRowsAtIndexPaths works, but the animation is glitchy. Is there another approach?

推荐答案

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

Why not just reload the cells directly? The indexPath contains the section and row so just keep track of which section you want to reload and fill the indexes in that array.

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

根据您的评论,您希望更改数组并让更改中的tableView动画。如果是这种情况,你应该考虑为UITableViews使用beginUpdates()和endUpdates(),甚至是 NSFetchedResultsController 因此它可以干净地处理所有更新动画。

Based on your comment you are looking to change your array and have the tableView animate in the changes. If that's the case you should consider using beginUpdates() and endUpdates() for UITableViews or even an NSFetchedResultsController so it handles all of the update animations cleanly.

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

我建议使用 NSFetchedResultsController 如果您正在使用Core Data,因为它简化了整个过程。否则,您必须自己处理更改。换句话说,如果您的阵列发生更改,则需要使用beginUpdates()和endUpdates()在tableview中手动删除或插入行。
如果您不使用核心数据,请参阅掌握它是如何处理的。

I'd recommend using NSFetchedResultsController if you're using Core Data as it simplifies this entire process. Otherwise you have to handle the changes yourself. In other words, if your array changes you need to manually remove or insert rows in the tableview using beginUpdates() and endUpdates(). If you aren't using Core Data, study this to grasp how it's all handled.

这篇关于使用动画重新加载表视图单元格(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆