Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 966
SentientBacon Avatar asked Jan 29 '15 03:01

SentientBacon


People also ask

How do you refresh a tableView cell?

You can use self. tableView. reloadData() in order to reload your entire table or self. tableView.

What does reload data do?

reloadData() :Reloads the rows and sections of the table view. Description : Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible.

How do you get the IndexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.


1 Answers

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) 
like image 101
Lyndsey Scott Avatar answered Sep 26 '22 10:09

Lyndsey Scott