UITableView with dynamic cell heights jumping when scrolling up after reloading cell

后端 未结 5 1506
孤街浪徒
孤街浪徒 2021-02-05 13:21

I have a table view with the potential for each cell to have its own height, thus isn\'t suitable to use rowHeight. Instead, right now I\'m using let indexSet

5条回答
  •  天命终不由人
    2021-02-05 13:52

    In iOS 13.5.1:

    I have a tableView which contains 4 types of cells , all off them having different and dynamic heights. I have followed the accepted solution here, but to solve jumping effect I need to add more when reloading table view:

    From accepted answer I have added below code: Declare this variable

    var allCellHeights = [IndexPath : CGFloat]()
    

    Then add 2 methods:

     func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
         self.allCellHeights[indexPath] = cell.frame.size.height
     }
     func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
         return self.allCellHeights[indexPath] ?? UITableView.automaticDimension
     }
    

    Now the extra code I have to add when reloading tableview:

    let contentOffset = self.tableView.contentOffset
    self.tableView.reloadData()
    self.tableView.layoutIfNeeded()
    self.tableView.setContentOffset(contentOffset, animated: false)
    

    also check this answer : reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling

提交回复
热议问题