UITableView's contentSize is wrong

on iOS 11 .I wtire this code:


- (void)viewDidLoad

{

[super viewDidLoad];

self.tableView.contentOffset = CGPointMake(0, _searchBar.height);

self.tableView.tableHeaderView = _searchBar;

}


After the tableView ‘layoutSubViews was executed, I get the tableView's contentSize.But the contentSize is wrong

Post not yet marked as solved Up vote post of ctinus Down vote post of ctinus
18k views

Replies

In iOS 11, table views use estimated heights by default. This means that the contentSize is just as estimated value initially. If you need to use the contentSize, you’ll want to disable estimated heights by setting the 3 estimated height properties to zero: tableView.estimatedRowHeight = 0 tableView.estimatedSectionHeaderHeight = 0 tableView.estimatedSectionFooterHeight = 0

  • I don't get it, why not just use the height that I give you explicitly. There's no need for estimation when I've given you the exact height for each and every index path. I just spent a day fixing a bug that was the result of this estimation logic. If all the cells are the same height — which isn't uncommon — then the estimation should just be the item count times the height. The estimation it was calculating for my table view was nearly twice the actual content size???

Add a Comment