UITableViewCell dynamic height breaks after insert row - ios

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

When I set the estimatedRowHeight to any value, then the problem disappears.
tableView.estimatedRowHeight = 44
in the viewDidLoad method completely solves my problem.

Section header view with automatic dimension may create this type of issues
So removing below lines will solve the problem.
tableView.sectionHeaderHeight = 10;
tableView.estimatedSectionHeaderHeight = UITableViewAutomaticDimension;
Row with Automatic Dimension will not create any issues.
tableView.estimatedRowHeight = 15;
tableView.rowHeight = UITableViewAutomaticDimension;

Related

UITableView cells disappearing, wrong cell passed to didEndDisplayingCell

I'm seeing another case of UITableView cells disappearing unexpectedly when scrolling, but none of the similar questions & answers here I've seen really fit what I'm seeing:
I scroll down until my top cell is removed and tableView:didEndDisplayingCell:forRowAtIndexPath: is called for it, then scroll back up until it's shown again. During the scroll up, the tableview is trying to remove the cells that were shown while scrolling down, however its actually removing the wrong cells.
As I'm scrolling up, tableView:didEndDisplayingCell:forRowAtIndexPath: is getting called with the correct index path, one it should be removing, but a cell that corresponds to a different index path. The cell and index path passed to the didEndDisplayingCell don't match.
This is in a small modification of the XLForm example project from https://github.com/xmartlabs/XLForm in which I've added a custom cell. What's weird is that if I make my custom cell the same height as the table's normal height (44) then this doesn't happen. Only when my cell is taller (64, with this value returned from tableView:heightForRowAtIndexPath: and tableView:estimatedHeightForRowAtIndexPath:).
I'm seeing this happen when running in the simulator, iOS versions 8.4 and 9.2.
I haven't been able to figure this out. I don't think I have any of the bugs that have caused others to have disappearing cell syndrome, but even if I do, I don't see how or why the tableview would be calling didEndDisplayingCell with cell and index path that don't match. Does anyone else have a clue what's happening?

UITableview not scrolling to the bottom cell if using UITableViewAutomaticDimension and estimatedRowHeight

I am facing a strange issue with UITableView if I enable the UITableViewAutomaticDimension property.
I am fetchign some data from the server and I have kept my UITableView cell set to accept dynamic content and increase the height as per the content so far what i have done is
Set the number of lines of the UILabel to zero so that it grows as per the content
Added the below code inside the viewDidLoad method
self.myTable.estimatedRowHeight = 200
self.myTable.rowHeight = UITableViewAutomaticDimension
Now after i fetch the data from the server i reload the table and go to the last cell of the table view but my problem is when i reload my entire table after the webservice call,
the table view animates and scrolls to the top of the screen and is not visible unless I scroll it down.
I searched and googled but i was not able to find any solution to this but if any of you have every came across this kind of situation please guide me out.
I have even added the below line of code before returning the cell in the cellForRowAtIndexPath but still am stuck with the same issue
cell.layoutSubviews()
here's the code which i have added to scroll to the bottom cell after the successful service call
self.myTable.scrollToRowAtIndexPath(NSIndexPath(forRow: self.myArray.count - 1, inSection: 1), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)

iOS dynamic height cell in tableview- issue with row 0 and 7

I am having a tableview in which I am displaying my data. As you can assume from the title, these cells are dynamic in their content, and so should their height be. Actually I got it working, but I have a strange issue with the row 0 and 7. These 2 rows take my entire screen, but when I scroll past them and go back again everything is fine. I am not sure but the 7. cell must be among the first cells that are being "dequed" when I scroll down. But as I said, this is just the first time they are being displayed, when I scroll again to one of them, everything is looking good (when filling the tableview with new data it is also fine).
I tried to reload the tableview twice when it is being populated for the first time, but without success. The presentation of the first cell I have fixed calling : self.tableView reloadAtIndexPaths ... when the table is being populated for the first time,
and it worked, it has the height it should have regarding its size. But I still have the issue with cell number 7 (the above fix does not work for this cell, I assume because it is not being displayed at that moment)
I am not using any fancy height calulation, just the iOS 8 feature:
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 80
Is there a method, fix with which i can force the tableview to draw the cells again ?
Thanks
Cell size is calculated from constrains. So I assume there is something wrong with those or with data during computation.
try this:
in tableview cell subclass use -prepareForReuse method to clean cell data as before reuse.

Table View Cells jump when selected on iOS 8

I'm trying to update my apps for iOS 8 and one strange issue that I can't figure out is that whenever I press a table view cell (on a grouped table view) and push another view controller on to the navigation controller, all the cells of the table view jump. It's sort of like they all reduce their height a little bit.
Consequently, when popping the navigation stack and coming back to the original table view screen, the cells jump again, this time they increase their height and going to back to how they were originally.
I have tried using a UITableViewController, doing my own table view management, not using explicity section heights, removing all custom header views and frames, basically anything I could think of.
It's happening on all the iOS 8 emulators and on actual devices runnings iOS 8 both iPhones and iPads.
Any suggestions will really help.
Edit: removed video
Alright, so if you're using the default UITableViewCells and setting custom fonts for the textLabel and detailTextLabel, make sure you either set rowHeight for the table view or implement one of the UITableViewDelegate methods for e.g. tableView:estimatedHeightForRowAtIndexPath:
The same code has worked fine on earlier versions of iOS, but needs fixing on iOS 8 for some reason.
I had the exact same problem!
It seems like the iOS 8 SDK uses the estimatedRowHeight, after selecting a row, which then results in a wrong height for the whole tableview, which causes the jumping of the tableview itself.
Not setting the estimatedRowHeight property or implementing the delegate method will fix the jumping bug.
I have not found a clean solution to fix it yet.
I solved the jumping problem with the Storyboard alone:
TableView Row Height: automatic.
TableView Estimate: my_value (e.g. 75)
TableView Cell Row Height: my_value
self.tableView.estimatedRowHeight=0;
self.tableView.estimatedSectionHeaderHeight=0;
self.tableView.estimatedSectionFooterHeight=0;
You can achieve self-sizing with this code:
implement together:
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = CGFloatValue (initial estimated height);
in your storyboard, add the correct constraint on you cells.
Note: the jump issue will still show sometimes.
the exact solution for this jump issue is implementing the delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return CGFloat;
}

Changing the height of the row causes issues

I got a problem with the Tableview-Cells in iOS.
I got a tableview with custon cells in it. I wanted to lower the height of the cells.
The row height in the tableview was set to 105, I changed it to 55 (the actual height of the custom cells wich are provided as xib-files).
So far, so easy, but now to my problem:
After the tableview loaded, it showed only one entry, but all the other entries aren't shown. If I scroll down and up again on the other hand, the previously hidden entries are shown.
I tried to work a little with the heigh-values in the tableview-options, and as I increase the heigh the more of a cell is shown on the first loading, but its never a complete cell until I set it to 105 again. Scrolling fixes the broken entries.
There is by far enough size on the screen and the cells height is exactly 55.
Has anyone here has an idea what could cause that problem?
As allways, thanks for any help.

Resources