Update specific Int-based UITableView string in Swift

I am starting a developer in Swift, and I am building a basic application that includes a UITableView. I want to update a specific row of a table using:

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

and I want the row to be updated to be specified in a row called rowNumber

The problem is that I don’t know how to do this, and all the threads I was looking for are for Obj-C

Any ideas?

+65
ios uitableview swift nsindexpath
Jan 29 '15 at 3:24
source share
10 answers

You can create NSIndexPath using the line and section number, and then reload it like this:

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

In this example, I assumed that your table has only one partition (i.e. 0), but you can change this value accordingly.

Update for Swift 3.0:

 let indexPath = IndexPath(item: rowNumber, section: 0) tableView.reloadRows(at: [indexPath], with: .top) 
+157
Jan 29 '15 at 3:30
source share

To solve soft impact animations:

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 your application from crashes:

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) } } 
+18
Apr 18 '16 at 7:22
source share

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 the TableView, then you can set the value of the section to 0.

+2
Jan 11 '17 at 6:02
source share
 let indexPathRow:Int = 0 let indexPosition = IndexPath(row: indexPathRow, section: 0) tableView.reloadRows(at: [indexPosition], with: .none) 
+2
Jun 07 '18 at 10:15
source share

Swift 4

 let indexPathRow:Int = 0 let indexPosition = IndexPath(row: indexPathRow, section: 0) tableView.reloadRows(at: [indexPosition], with: .none) 
+1
Apr 27 '18 at 5:36
source share

What about:

 self.tableView.reloadRowsAtIndexPaths([NSIndexPath(rowNumber)], withRowAnimation: UITableViewRowAnimation.Top) 
0
Jan 29 '15 at 3:31 on
source share

In addition, if you have partitions to view the table, you should not try to find all the rows that you want to update, you should use reload sections. This is a simpler and more balanced process:

 yourTableView.reloadSections(IndexSet, with: UITableViewRowAnimation) 
0
Jul 11 '18 at 6:11
source share

Swift 4.1

use it when deleting a 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) 
0
Oct 18 '18 at 13:58 on
source share

I understand that this question is for Swift, but here is Xamarin equivalent code for the accepted answer if anyone is interested.

 var indexPath = NSIndexPath.FromRowSection(rowIndex, 0); tableView.ReloadRows(new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Top); 
0
Jan 10 '19 at 16:22
source share

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) } 
0
Feb 05 '19 at 21:03
source share



All Articles