Dynamic Height UITableViewCell data wrong order - ios

Is it appropriate to call [tableView beginUpdates] and [tableView endUpdates] in cellforRowAtIndexPath?
I am trying to dynamically expand cell heights based on the content within the cell. When I use [tableView beginUpates] and [tableView endUpdates] in cellForRowAtIndexPath, the output looks ok. However, once I start scrolling, data in the cells becomes ordered incorrectly and sometimes the bar separator between table cells disappears.
Should I not be using [tableView beginUpdates] and [tableView endUpdates] for this?
Thanks!

You've got to specify your dynamic cell heights in heightForRowAtIndexPath because the table view wants to know its content size up-front. Any changes you make to the cell's frame in cellForRowAtIndexPath will be overridden by the table view, so what you're attempting won't work.
If your heights are derived from the cell's content, consider using the prototype approach outlined here. Also, if you can use a 3rd party library, check out TLIndexPathTools. It does dynamic cell height for free if your custom cell class implements the TLDynamicSizeView prototype. Try running the Dynamic Height sample project.

Related

Changing Constraints of UITableViewCell, not affecting TableViewCell height in TableView

I am changing the constraint for Cell, But the height of the Cell remains same till deque, after dequeuing it gives correct result.
Any idea how can we reload the cell if the cell constraints are manipulated?
Using Rxswift for Cell View Model, but TableView is in Objc.
Thanks
It is always nice to share your code.
How are you changing the height of your cell? Are you using the heightForRowAtIndexPath delegate.
For changes to appear, I use this code.
[self.tableView beginUpdates];
[self.tableView endUpdates];
If this does not work for you, please share your code and I would update my answer accordingly.

reloadRowsAtIndexPaths updates the contentOffset Of the table view

I am trying to reload a particular row from the tableview but the contentOffset of the tableview is reset to (0, 0) after the reload. I tried to add [tableview beginUpdates] and [tableview endUpdates] around the reloadRowsAtIndexPaths but didn't change the behavior.
The question was asked over here Calling reloadRowsAtIndexPaths removes tableView contentOffset but it did not solve the problem. I am pretty sure that animation has nothing to do with this behavior. I am not sure how to maintain the content Offset of the tableview while still reloading the tableview row.
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
I'm guessing you've implemented tableView:estimatedHeightForRowAtIndexPath: and have it returning 0.
After reloadRowsAtIndexPaths:withRowAnimation:, the tableView determines its offset using the estimated heights given in tableView:estimatedHeightForRowAtIndexPath:. So unless the value you return there is accurate, implementing it will cause your tableView's offset to change after the reload. I had a similar problem and fixed it by removing my implementation of tableView:estimatedHeightForRowAtIndexPath:.
Updating the estimatedRowHeight and estimatedSectionHeaderHeight with more accurate values can also help alleviate this issue if you haven't implemented any of the estimated height delegate methods.
You can use the view inspector to find the heights of your cells and headers.

How can I aesthetically resize a UITableview's Cell when I type?

Hi I'm trying to resize a UITableView's UITableViewCell as I type in a TextView in the cell. I want the text to keep flowing but focus on what I"m typing now. This is similar to what you would experience in the Mail App. What is the best way to resize the cell dynamically as I keep typing and entering content?
I'm assuming you are already sizing your cell dynamically.
Whenever the user types a character do this...
[tableView beginUpdates];
[tableView endUpdates];

Dequeued UICollectionViewCell not refreshed

I have an application with a UICollectionView with horizontal scroll of cells (Only one cell is displayed at a time). Each cell hosts a UITableView which displays content (URL links) in each of the UITableViewCell.
The problem is that the first two cells of UICollectionView display the proper content, but starting from the third cell, the contents are displayed from the dequeued cells. The correct content on the dequeued cell's UITableView is only displayed if user scrolls down and then scrolls up to the top.
For example, the contents of the third cell are the content from the first cell.
The cell is a custom UICollectionViewCell that hosts the UITableView. The custom cell implements the UITableViewDataSource, UITableViewDelegate.
I have searched through different forums and sites and found the solution of refresh.
I tried adding the [collectionView reload] to the -cellForItemAtIndexPath: method, but it goes into constant loop. I know that is not the right place, but i do not have any other methods that cause the refresh (only scrolling through the cells loads the next cell's content).
Can somebody please help me to resolve the issue?
You are missing some facts about dequeuing.
When you 3d custom UICollectionViewCell is dequeued, it isn't allocated like when it's displayed for the first time. Instead, your collection view "reuses" 1st cell for displaying 3d. The best moment to clear content in your custom cell class is -(void)prepareForReuse. If you override it and clear it's content, the -cellForItemAtIndexPath: will dequeue "clear" cell which you can further customize.
However, i don't exactly know how to "clear" the tableView, not sure whether it's possible. You have to reloadData on your tableView in -cellForItemAtIndexPath:. Example:
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
MyCVCell *cell = [collectionView dequeueReusableCellWithIdentifier:#"MyCVCell" forIndexPath:indexPath];
cell.arrayOfURLsForDisplay = self.arrayOfArraysOfURLs[indexPath.row];
[cell.tableView reloadData];
return cell;
}
Here i mean you have some arrayOfArraysOfURLs in your viewController and you dislay URLs in your custom cell from some arrayOfURLsForDisplay
I had similar problem, instead of using [cell.tableView reloadData] inside the cellForItemAtIndexPath, I called collectionView.reloadData in the prepareForUse method that is overwritten in your custom UITableviewCell

Can the height of a UITableViewCell be changed without reloading the table view?

I'm trying to resize a UITableViewCell without reloading the table view.
My UITableViewCell contains a UITextView so users can input list items that may be several lines (about four max) in length. Right now, I'm resizing the text view each time the user presses the return key, but I'd like to change the height of the table view cell as well.
The main problem I'm running into is that reloading the table view's data makes the text view lose focus. Is there any way to change the height of the UITableViewCell without reloading the table view, letting the user continue to input text in the text view?
To resize a table view row, try sending:
[tableView beginUpdates];
[tableView endUpdates];
These messages one immediately after the other should do the trick provided you have the correct height returned for that cell in the tableView:heightForRowAtIndexPath: method. The row should animate. You should be able to return the height of your cell using its bounds or just from knowing what you've set the cell height as.
Obviously "tableView" is the name of your table view.

Resources