ChatGPT解决这个技术问题 Extra ChatGPT

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?

Is there only one section?

C
Carmen

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)

if for particular section number of rows are going to be changed then ? @Lyndsey Scott
@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 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...
Where to call this method?
K
Khuong

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

N
Neha

Swift 4

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

Why you copy paste my answer ?
You had copied my answer, check date
L
Legend_ 33

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

j
jaiswal Rajan

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.


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

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.
B
Burcu Kutluay

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)

w
wildbagel

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

s
skyline75489

How about:

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

R
Rana Ali Waseem

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
irwin B
    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()

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.
Please add all clarification to your answer by editing it