100

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?

SentientBacon
  • 1,509
  • 3
  • 11
  • 19

11 Answers11

219

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)
Carmen
  • 6,177
  • 1
  • 35
  • 40
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
  • if for particular section number of rows are going to be changed then ? @Lyndsey Scott – dip Aug 05 '15 at 10:00
  • @Lyndsey : Actually at first lets say i have 2 cells in that section of which i am going to reload, after reloading lets say i will have any (x) numbers of cells in that particular section, so my question is that in line of indexpath calculation, iam sure about the section but i am confused on numbers of rows, because it is crashing on change of number of rows **var indexPath = NSIndexPath(forRow: rowNumber, inSection: 0)** – dip Aug 06 '15 at 03:54
  • @dip you need to look at your tableView:numberOfRowsInSection: method algorithm to get that info... I'd recommend posting your question on the forum since it sounds like a specific issue you're having with your code... – Lyndsey Scott Aug 06 '15 at 08:24
  • Where to call this method? – Master AgentX Feb 27 '19 at 08:54
22

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)
   }
}
Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
7

Swift 4

let indexPathRow:Int = 0    
let indexPosition = IndexPath(row: indexPathRow, section: 0)
tableView.reloadRows(at: [indexPosition], with: .none)
Neha
  • 666
  • 1
  • 10
  • 17
4

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)
    }
Legend_ 33
  • 125
  • 6
3

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.

jaiswal Rajan
  • 4,641
  • 1
  • 27
  • 16
2
let indexPathRow:Int = 0
let indexPosition = IndexPath(row: indexPathRow, section: 0)
tableView.reloadRows(at: [indexPosition], with: .none)
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
Sachin Rasane
  • 1,501
  • 12
  • 17
  • 1
    Hi, Welcome to Stack Overflow and thank you for your first answer. To make the answer more useful to other people, it is best practice to annotate your answer with text that explains why it addresses the OP's original question. – Spangen Jun 07 '18 at 10:18
2

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);
wildbagel
  • 145
  • 8
1

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)
Burcu Kutluay
  • 472
  • 6
  • 14
1
    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()
irwin B
  • 21
  • 2
  • I found myself in a particular situation. My main goal was to accomplish that when the user is in a cell. And I have made some modification. You can update only the cell, it is in, without loading the whole table and confusing the user. For this reason, this function takes advantage of the tableView properties, adding a custom ReloadDataCell. And adding tableview.reloadDataSavingSelecctions (). Where, you do some action. And once I did, I wanted to share that solution with you. – irwin B May 02 '20 at 02:33
  • Please add all clarification to your answer by editing it – Nico Haase May 02 '20 at 06:51
0

How about:

self.tableView.reloadRowsAtIndexPaths([NSIndexPath(rowNumber)], withRowAnimation: UITableViewRowAnimation.Top)
ljk321
  • 16,242
  • 7
  • 48
  • 60
0

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)