Search code examples
iosuitableviewswiftnsindexpath

Refresh certain row of UITableView based on Int in Swift


I am a beginning developer in Swift, and I am creating a basic app that includes a UITableView. I want to refresh a certain row of the table using:

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

and I want the row that will be refreshed to be from an Int named rowNumber

The problem is, I don't know how to do this and all the threads I've searched are for Obj-C

Any ideas?


Solution

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