iOS TableView reload and scroll top

后端 未结 6 548
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 07:50

the second day I can not solve the problem with the table.

We have a segmentedControl which, when changed, changes the table. Suppose that there are 3 elements in th

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 08:07

    Okay. So people finding issue with scrolling a tableview to the top with

    self.tableView.setContentOffset(.zero, animated: false)
    

    And need to do reload data unnecessarily to fix this !

    So What is the issue with setContentOffset is kind of bug of iOS ?
    The smallest and simplest answer is NO so what's the issue then ?

    The answer is that dynamic row is using UITableView.automaticDimension. Tableview calculates its content offset with row height and with the number or sections and rows you have in your data source. So in case of dynamic height is not performing the scrollToTop correctly, it is because it needs to recalculate every row height and content size.

    So to reduce this bundle from tableview we, can provide an estimated row height like

    self.tableView.estimatedRowHeight = 117 // whatever your value 
    

    Hopefully this will help someone :)

提交回复
热议问题