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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29632472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:36:58  来源:igfitidea点击:

Reload Table view cell with animation (Swift)

iosswiftrowtableviewreloaddata

提问by William Larson

Is there a way to reload specific UITableViewcells 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(),但动画有问题。还有另一种方法吗?

回答by Mark McCorkle

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 NSFetchedResultsControllerso it handles all of the update animations cleanly.

根据您的评论,您希望更改数组并在更改中对 tableView 进行动画处理。如果是这样的话,你应该考虑使用beginUpdates()和endUpdates()用于UITableViews甚至是NSFetchedResultsController所以它处理所有的更新动画干净。

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

I'd recommend using NSFetchedResultsControllerif 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 thisto grasp how it's all handled.

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

回答by jaiswal Rajan

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)

回答by alessioarsuffi

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)

回答by Junhang Lv

the define IndexSetin official document:

官方文档中定义IndexSet

Manages a Setof 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.

管理Set整数值,在 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)