Refresh certain row of UITableView based on Int in Swift

前端 未结 11 534
别跟我提以往
别跟我提以往 2020-12-23 02:50

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.tabl         


        
相关标签:
11条回答
  • 2020-12-23 03:11

    Swift 4

    let indexPathRow:Int = 0    
    let indexPosition = IndexPath(row: indexPathRow, section: 0)
    tableView.reloadRows(at: [indexPosition], with: .none)
    
    0 讨论(0)
  • 2020-12-23 03:18

    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)
       }
    }
    
    0 讨论(0)
  • 2020-12-23 03:18

    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)
    
    0 讨论(0)
  • 2020-12-23 03:21

    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)
    
    0 讨论(0)
  • 2020-12-23 03:28

    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 讨论(0)
  • 2020-12-23 03:29

    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);
    
    0 讨论(0)
提交回复
热议问题