Search code examples
iosuitableviewswiftautolayout

UITableViewCell dynamic height breaks after insert row


I am trying to make my first iOS App with swift (iOS 8).

I have a UITableView and a CustomCell. The app is kind of a news ticker. After launching the app it loads all news from a web server and saves the news to an array. The custom cell has a dynamic height. I am calling cell.layoutIfNeeded() in the tableView:cellForRowIndexPath to get the dynamic cell height correctly working. Otherwise the cell height is not displayed correctly. I found that hint here at stack overflow. Image: After getting all news, the cells are displayed correctly

With SIOSocket I get an event with a new news and I want to insert it into the UITable. So I created a method that inserts the news into my array and calls the tableView.insertRowsAtIndexPath in between beginUpdates(), endUpdates(). And here is where the problem starts. The new cell is displayed correctly, but all the other visible cells are downsized to the default cell height (44pt). Image: After I added a new entry, the old cells are too small After some scrolling the cells are updated and are now displayed correctly again.

When I do not use the insertRowsAtIndexPath, but tableView.reloadData(), then it's working. But I think it's not good style to call reloadData for every new news. And of course there is no animation after reloading...

Can anybody please explain why this happens and is there any solution to fix that issue? Thanks


Solution

  • When I set the estimatedRowHeight to any value, then the problem disappears.

    tableView.estimatedRowHeight = 44
    

    in the viewDidLoad method completely solves my problem.