使用动画重新加载表视图单元格(Swift)

时间:2022-11-29 09:12:08

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

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

I've been using:

我一直在用:

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#>)

EDIT:

编辑:

After using reloadRowsAtIndexPaths

使用reloadRowsAtIndexPaths之后

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).'

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

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

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

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

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

4 个解决方案

#1


10  

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.

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

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

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.

根据您的评论,您希望更改阵列并在更改中使tableView具有动画效果。如果是这种情况,您应该考虑对UITableViews甚至NSFetchedResultsController使用beginUpdates()和endUpdates(),以便它可以干净地处理所有更新动画。

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

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.

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

#2


4  

In Swift 3.0

在Swift 3.0中

We can reload particular row of particular section in tableview

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

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

#3


2  

If you want to reload single section in swift 3.0 you can do this:

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

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

#4


2  

the define IndexSet in official document:

在官方文档中定义IndexSet:

Manages a Set of integer values, which are commonly used as an index type in Cocoa API. The range of valid integer values is in (0, INT_MAX-1). Anything outside this range is an error.

管理一组整数值,这些值通常用作Cocoa API中的索引类型。有效整数值的范围是(0,INT_MAX-1)。超出此范围的任何内容都是错误的。

so IndexSet is Set about index, just putting some Int values in [ ]

所以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)

#1


10  

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.

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

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

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.

根据您的评论,您希望更改阵列并在更改中使tableView具有动画效果。如果是这种情况,您应该考虑对UITableViews甚至NSFetchedResultsController使用beginUpdates()和endUpdates(),以便它可以干净地处理所有更新动画。

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

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.

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

#2


4  

In Swift 3.0

在Swift 3.0中

We can reload particular row of particular section in tableview

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

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

#3


2  

If you want to reload single section in swift 3.0 you can do this:

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

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

#4


2  

the define IndexSet in official document:

在官方文档中定义IndexSet:

Manages a Set of integer values, which are commonly used as an index type in Cocoa API. The range of valid integer values is in (0, INT_MAX-1). Anything outside this range is an error.

管理一组整数值,这些值通常用作Cocoa API中的索引类型。有效整数值的范围是(0,INT_MAX-1)。超出此范围的任何内容都是错误的。

so IndexSet is Set about index, just putting some Int values in [ ]

所以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)