在 Swift 中基于 Int 刷新 UITableView 的某一行

我是 Swift 的初级开发者,我正在创建一个包含 UITableView 的基本应用程序。我想用以下方法刷新表中的某一行:

self.tableView.reloadRowsAtIndexPaths(paths, withRowAnimation: UITableViewRowAnimation.none)

并且我希望将被刷新的行来自一个名为 rowNumber 的整数

问题是,我不知道如何做到这一点,而且我搜索的所有线程都是针对 Obj-C 的

有什么想法吗?

136027 次浏览

You can create an NSIndexPath using the row and section number then reload it like so:

let indexPath = NSIndexPath(forRow: rowNumber, inSection: 0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)

In this example, I've assumed that your table only has one section (i.e. 0) but you may change that value accordingly.

Update for Swift 3.0:

let indexPath = IndexPath(item: rowNumber, section: 0)
tableView.reloadRows(at: [indexPath], with: .top)

How about:

self.tableView.reloadRowsAtIndexPaths([NSIndexPath(rowNumber)], withRowAnimation: UITableViewRowAnimation.Top)

For a soft impact animation solution:

Swift 3:

let indexPath = IndexPath(item: row, section: 0)
tableView.reloadRows(at: [indexPath], with: .fade)

Swift 2.x:

let indexPath = NSIndexPath(forRow: row, inSection: 0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

This is another way to protect the app from crashing:

Swift 3:

let indexPath = IndexPath(item: row, section: 0)
if let visibleIndexPaths = tableView.indexPathsForVisibleRows?.index(of: indexPath as IndexPath) {
if visibleIndexPaths != NSNotFound {
tableView.reloadRows(at: [indexPath], with: .fade)
}
}

Swift 2.x:

let indexPath = NSIndexPath(forRow: row, inSection: 0)
if let visibleIndexPaths = tableView.indexPathsForVisibleRows?.indexOf(indexPath) {
if visibleIndexPaths != NSNotFound {
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}

In Swift 3.0

let rowNumber: Int = 2
let sectionNumber: Int = 0


let indexPath = IndexPath(item: rowNumber, section: sectionNumber)


self.tableView.reloadRows(at: [indexPath], with: .automatic)

byDefault, if you have only one section in TableView, then you can put section value 0.

Swift 4

let indexPathRow:Int = 0
let indexPosition = IndexPath(row: indexPathRow, section: 0)
tableView.reloadRows(at: [indexPosition], with: .none)
let indexPathRow:Int = 0
let indexPosition = IndexPath(row: indexPathRow, section: 0)
tableView.reloadRows(at: [indexPosition], with: .none)

In addition, If you have sections for tableview, you should not try to find every rows you want to refresh, you should use reload sections. It is easy and more balanced process:

yourTableView.reloadSections(IndexSet, with: UITableViewRowAnimation)

Swift 4.1

use it when you delete row using selectedTag of row.

self.tableView.beginUpdates()


self.yourArray.remove(at:  self.selectedTag)
print(self.allGroups)


let indexPath = NSIndexPath.init(row:  self.selectedTag, section: 0)


self.tableView.deleteRows(at: [indexPath as IndexPath], with: .automatic)


self.tableView.endUpdates()


self.tableView.reloadRows(at: self.tableView.indexPathsForVisibleRows!, with: .automatic)

I realize this question is for Swift, but here is the Xamarin equivalent code of the accepted answer if someone is interested.

var indexPath = NSIndexPath.FromRowSection(rowIndex, 0);
tableView.ReloadRows(new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Top);

SWIFT 4.2

    func reloadYourRows(name: <anyname>) {
let row = <your array name>.index(of: <name passing in>)
let reloadPath = IndexPath(row: row!, section: 0)
tableView.reloadRows(at: [reloadPath], with: .middle)
}
    extension UITableView {
/// Reloads a table view without losing track of what was selected.
func reloadDataSavingSelections() {
let selectedRows = indexPathsForSelectedRows


reloadData()


if let selectedRow = selectedRows {
for indexPath in selectedRow {
selectRow(at: indexPath, animated: false, scrollPosition: .none)
}
}
}
}


tableView.reloadDataSavingSelections()