Swift ios 9: Section header change position after reload data

后端 未结 7 1074
我在风中等你
我在风中等你 2021-01-17 14:53

I have plain UITableView with many sections and rows. Sections work fine. But sometimes after reload data of table, section change position. For example it was happened when

相关标签:
7条回答
  • 2021-01-17 15:02

    It looks like the issue in estimated height for your table view cells. Due to internal optimisations system calculates the height of absent cells based on estimated height value, that's why it can position your headers on wrong calculated height (like real cells bigger or smaller than their estimated size). It usually happens when you call reloadData() in the middle of the list. As a proper solution you should avoid calling reloadData() when you want to update the content of the list with second page for example. So you should use tableView.beginUpdates() and tableView.endUpdates() and update items collection for table view. There is also new closure based API performBatchUpdates (just like it was for UICollectionView), but it's available from iOS 11.

    Hope it may help to find proper solution for you

    0 讨论(0)
  • 2021-01-17 15:10

    In my project ,this error happened when i pull up tableView to request more data then reload the data after requesting. I need create many sections after requesting the data and it may effect the performance if i reloadData twice. More than, i found that if i scroll up or down the tableView, the position of section's headerView get right. So i fix this bug(my condition) i requesting the data by this:

    [_tableView reloadData];
    CGPoint offSetOrigin     = _tableView.contentOffset;
    CGPoint offSetSecond     = CGPointMake(0, offSet.y-1);
    _tableView.contentOffset = offSetSecond;
    _tableView.contentOffset = offSet;
    
    0 讨论(0)
  • 2021-01-17 15:10

    The solutions here didn't fix my problem, so I'm leaving one more possible solution that helped me.

    Set the estimatedSectionHeaderHeight, it seems that my tableView wasn't being able to calculate the initial position of my first section without it.

    0 讨论(0)
  • 2021-01-17 15:12

    I know it's an old thread but uncheck the Adjust Scroll View Insets doesn't help. I had this problem and solved it by reload table twice.

    0 讨论(0)
  • 2021-01-17 15:15

    Calling reloadData() twice also works for me, but I would suggest calling tableView.layoutSubviews() after reloadData(), which also works for me.

    0 讨论(0)
  • 2021-01-17 15:21
    tableView.reloadData()
    tableView.layoutIfNeeded()
    tableView.beginUpdates()
    tableView.endUpdates()
    

    This help me out.

    Double reloadData will cause flickering while this solution won't.

    0 讨论(0)
提交回复
热议问题