How does UITableView manage cells with variable height - ios

How does UITableView manages cell with variable height? it asks for height of each and every cell/row on load, but moving further how does it "INTERNALLY" ...
Calculate y origin of each and every cell. Calculating one after another is not an issue.
Decide which cell might be lying in a rect given some arbitrary contentOffset. What and how does it maintain its data structure to perform some fast calculation irrespective of number of rows in table view.
How does it manage addition and deletion of rows and update its data structure without much overhead .
Can someone shed light on this... thanks

Basically you need to be careful using cells with variable height, as it can be a performance problem. Your heightForRowAtIndexPath method can be called for cells that are not currently visible, which can be a very large number in tables with a lot of content. Calculating the origins works just like you imagine it would - it needs to know the heights of all cells above the one it's calculating the origin for.

For cells editing you need to add this
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
When you perform the deletion operation it removes the content from the datasource array which you are using in the cellForRowAtIndexPath method .

Related

IOS/Objective-C: Reusing Cell Throws off Autolayout of Different Size Elements in TableviewCell

I am displaying feed items in a tableview. Depending on the item, the cell must be larger or smaller--for example larger to accomodate a photo or larger still if it references another feed item such as an article and I have to display the item referenced.
In most cases, I've used autolayout to get the cells to auto expand or contract based on the content. However, in a couple cases, this was proving next to impossible so I reset the value of a constraint in code. For example, if there is no photo in one case, I shrink the height of the cell in code as the cell was not shrinking on its own.
This works fine when the page loads. But when you scroll and reuse cells, it leads to weird effects such as the cell expanding vertically or lines being pushed into one another.
Some questions on SO suggest using the method:
-(void) tableView:(UITableView *)tableView didEndDisplayingCell:(IDFeedCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//Undo changes here
}
The problem is I don't know how to undo changes. For example, if I made a cell smaller by setting
self.height.constant = 100;
Should I set it back to the constant value in storyboard?
What value can I set it to when I don't know what the next item to resuse the cell will need in the way of dimensions.
Thanks for any suggestions. BTW, autolayout shows no issues.

Resize UITableView to fit all cells

I am using a UITableView within an auto-sized UITableViewCell to display a variable, but small number of items. I want the nested UITableView to expand to display all of its rows. (I know this may not be the best way to do things, so please bear with me on the premise.)
The problem I'm having is that the UITableView does not have an intrinsic size based on the size of all of its cells. Is there any way to force all of the cells to load an then compute an intrinsic size? (Or some other way to have a full-sized nested UITableView.)
Thanks!
There is a method called
- (void)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
where you define the height of each cell. I believe the default is 44. You can just take those values and then multiply them by the number of cells you're populating and that will be the height your UITableView should be.

UITableView dynamic cell heights - reload height for single row

I have a UITableView with rows of dynamic heights, many of which contain UITextViews. When the user starts typing in one of the textviews, I have the cells grow to accommodate the size of the textview content using the begin/endUpdates method. Using this method allows the cells to resize without losing keyboard focus on the textview, an important aspect of my app.
However, when I call begin/endUpdates, it reloads the heights for every cell that I have, and I was wondering if there was any way to only recompute the height of a cell at a particular indexPath while the user is typing. I want to do this because my heights for the other cells are expensive to compute as they have dynamic content. I know I could write some height caching code, but I was wondering if there was any method to only recompute the height of a specific cell or set of cells in a UITableView without losing keyboard focus / reloading the data content of that row?
I am using ios8, but will take an ios7/8 solutions.
Create of array of NSIndexPath's. If you have just one indexPath, add just this to the array, and use this method:
[self.tableView reloadRowsAtIndexPaths:[NSArray *array] withRowAnimation:UITableViewRowAnimationAutomatic];
but i think this method, will also hide the keyboard, never tried.
Doesn't seem like this is possible to do for only a single row. I ended up building a caching system for row heights so that at least it is faster to return heights for all cells.

Getting appropriate UITableViewCell separations: GUI

I have a UITableViewController that is segue-embedded with a UIContainerView. I'm having trouble appropriating the spacings located between the three given UITableViewCell's.
-- I need another space above the very top cell and between the 2nd and 3rd UITableViewCell, I'm having a hard time finding out how to accomplish this.
In your UITableViewDelegate, add this following method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
From there you can control the total height of each cell independently. Don't do this for large tables, however, as it slows things down.
If you are not using section headers (or footers) already, you can use them to add arbitrary spacing to table cells. Instead of having one section with n rows, create a table with n sections with one row each.
Implement the tableView:heightForHeaderInSection: method to control the spacing.
You may also want to implement tableView:viewForHeaderInSection: to control what the spacing looks like.

Change cell height while scrolling a UITableView

Is there anyway to change the height of a cell dynamically whilst scrolling a UITableView?
I need to change the height of a number of cells when the scroll position reaches a certain point as the user drags it down past the top. I can do this successfully by issuing a reloadData, however it's very abrupt since the cells just vanish.
I've also tried reloading the cells and running the begin/end updates on the table view, however in both cases the animation sends the scroll view back to the top.
I'd like to animate the cell height change without messing up the current content offset of the drag in progress.
Tim
Use the UITableViewDelegate and implement the method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
This solve your issue?
If it is not solving your issue, try to explain better your goal, with examples/images.

Resources