Refresh certain row of UITableView based on Int in Swift

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

    How about:

    self.tableView.reloadRowsAtIndexPaths([NSIndexPath(rowNumber)], withRowAnimation: UITableViewRowAnimation.Top)
    
    0 讨论(0)
  • 2020-12-23 03:33

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

    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.

    0 讨论(0)
  • 2020-12-23 03:34
        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()
    
    0 讨论(0)
  • 2020-12-23 03:35
    let indexPathRow:Int = 0
    let indexPosition = IndexPath(row: indexPathRow, section: 0)
    tableView.reloadRows(at: [indexPosition], with: .none)
    
    0 讨论(0)
提交回复
热议问题