Why does my UITableView “jump” when inserting or removing a row?

后端 未结 10 1196
小蘑菇
小蘑菇 2020-12-05 10:21

(Happy to accept an answer in Swift or Objective-C)

My table view has a few sections, and when a button is pressed, I want to insert a row at the end of section 0.

10条回答
  •  鱼传尺愫
    2020-12-05 10:30

    I don't know how to fix it correctly, but my solution works for me

    // hack: for fix jumping of tableView as for tableView difficult to calculate height of cells
        tableView.hackAgainstJumping {
          if oldIsFolded {
            tableView.insertRows(at: indexPaths, with: .fade)
          } else {
            tableView.deleteRows(at: indexPaths, with: .fade)
          }
        }
    
    
    extension UITableView {
      func hackAgainstJumping(_ block: () -> Void) {
          self.contentInset.bottom = 300
          block()
          self.contentInset.bottom = 0
      }
    }
    

提交回复
热议问题