Reload Table view cell with animation (Swift)

谁都会走 提交于 2019-12-03 03:00:52

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)

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

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.

In Swift 3.0

We can reload particular row of particular section in tableview

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

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

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

the define IndexSet in official document:

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.

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

// 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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!