Refresh certain row of UITableView based on Int in Swift

前端 未结 11 696
别跟我提以往
别跟我提以往 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: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)
       }
    }
    

提交回复
热议问题