Refresh certain row of UITableView based on Int in Swift

IosUitableviewSwiftNsindexpath

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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)

Solution 2 - Ios

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

Solution 3 - Ios

Swift 4

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

Solution 4 - Ios

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.

Solution 5 - Ios

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

Solution 6 - Ios

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

Solution 7 - Ios

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)

Solution 8 - Ios

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

Solution 9 - Ios

How about:

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

Solution 10 - Ios

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)

Solution 11 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSentientBaconView Question on Stackoverflow
Solution 1 - IosLyndsey ScottView Answer on Stackoverflow
Solution 2 - IosAlessandro OrnanoView Answer on Stackoverflow
Solution 3 - IosNehaView Answer on Stackoverflow
Solution 4 - Iosjaiswal RajanView Answer on Stackoverflow
Solution 5 - IosLegend_ 33View Answer on Stackoverflow
Solution 6 - IosSachin RasaneView Answer on Stackoverflow
Solution 7 - IosBurcu KutluayView Answer on Stackoverflow
Solution 8 - IoswildbagelView Answer on Stackoverflow
Solution 9 - Iosskyline75489View Answer on Stackoverflow
Solution 10 - IosRana Ali WaseemView Answer on Stackoverflow
Solution 11 - Iosirwin BView Answer on Stackoverflow