How to set dynamic height of cell based on content - ios

I am have an app which requires to display user comments within a table cell. How can I set the dynamic height of the cell based on the contents of the cell like in Instagram?
Basically, I have texts and comments that have different length in different cells. Can anyone advise me on how I can implement the dynamic height method (like instagram)?

Check the UITableViewDelegate documentation.
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
Bear in mind you can't reference the cell from this method, so you want to calculate the height of the contents separately and use it to generate the height.

Combine my dynamic label category here with my dynamic table cell height here. Perfect combination!

You should give a look at three20 library source code, which offers complex cells like you want to do. It might give you answers on how to do this, even if you do not intend to use it:

You should refer to this question. The answer is here.
Dynamic UITableView height in UIPopoverController (contentSizeForViewInPopover)?

Related

Inferring row height in heightForRowAtIndexPath

What I would like to do:
In the UITableViewController method heightForRowAtIndexPath I would like to infer the value of the row height rather than hardcoding it.
I set the value previously in the storyboard in Interface builder.
Wrong approach:
The following method is the wrong approach as it is called before the actual cell is created and hence it loops:
// wrong approach
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//AppTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"AppCel" forIndexPath:indexPath];
//int height = cell.frame.size.height;
// TODO: Make it more dynamic
return 112;
}
Approach I am thinking of:
Is there a way to load the cell main.storyboard document and infer the height value? Or is there some other approach?
Related questions search:
I found the following unanswered related question dating back to 2010. This other question is not of much use as it derives the height from the cell content rather than from the story board document.
The approach you had coded is actually not totally wrong. What is wrong is using dequeueReusableCellWithIdentifier:forIndexPath. Instead you want to use the simpler version dequeueReusableCellWithIdentifier which you can use to provide a prototype cell which you can configure, layout and then calculate the height.
If using purely iOS8, all this is done for you with the new auto layout. If you want to support iOS7 as well, then you need to do this manually.
The following link explains the process you need to use. I use this and it works great when you want to support both iOS7 and iOS8.
Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
You also have to watch if you use size class based constraints in your cells as there is a less than obvious issue you have to work around to get the height correct. See Offscreen UITableViewCells (for size calculations) not respecting size class?
Yes you can.
You can use autolayout constraints and let the autolayout do the calculations for you.
check out AppCoda tutorial for this Link
If you have the cell in a xib (CP_Cell.xib) you can do that:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return ((CP_Cell *) [[NSBundle mainBundle] loadNibNamed:#"CP_Cell" owner:self options:nil].lastObject).frame.size.height;
}

UICollectionViewCells with dynamic heights

What would the pattern be for sizing a UICollectionViewCell based on some content I've downloaded? I wouldnt know the size when the cell gets created - it would be sometime after my content has downloaded that the cell would callback with its height. What would I call on the collectionview to have it re layout its cells?
I've faced this with tableviews and ended up calculating the height based on the strings and controls in the cell. But in that case essentially everything was a function of data already in my model. Thats not the case here.
Thanks,
Jason
Depending on the number of cells you will displaying (e.g. a small # of cells), simply calling [UICollectionView reloadData] will do the trick once you know the different heights from that network callback.
I think that the the RFQuiltLayout can solve your problem.
Use the function
(CGSize) blockSizeForItemAtIndexPath:(NSIndexPath *)indexPath
Here is the link https://github.com/bryceredd/RFQuiltLayout

Trouble with iOS group table

I'm new to iOS programming, and I'm struggling to get a table to look like the one I've designed. I'm using a grouped table, but I can't figure out how to get varying height and how to reduce the padding. Does anyone know a better way to do this than a grouped table? I've attached a link to the image showing what I'm trying to do.
https://docs.google.com/leaf?id=0Bx7qAEhjSDyPYTQyYWUxNjMtMDIxNi00OGM0LTgxODgtMWIxMTcwYmJjNjI3&hl=en_US
That looks like you want to use a one row per section and a different height for each section's row.
The varying height comes from using a different custom cell for each section.
To get individual row height:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Padding is more complex, you have to override UITableView. See this question.

UITableViewCells

Can tableview cells be dynamically configured so different cells can have different heights, based on data/text content? Does this violate Human Interface Guidelines? The documentation I have found is spotty and confusing, at least to newbies like me! :)
You can specify the height for each cell by implementing the tableView:heightForRowAtIndexPath: method in your UITableViewDelegate; note this will be called for every row, even rows not currently visible, so it must be fast and you may have trouble if your table has thousands of rows.
The only statement I see regarding cells with different heights in the HIG is "Avoid variable row heights in a plain table. Variable row heights are acceptable in grouped tables, but they can make a plain table look cluttered and uneven."
The UITableViewCell does not control its own height.
To set the height of a row in the table, implement the UITableViewDelegate method tableView:heightForRowAtIndexPath:
This can be somewhat painful sometimes - since often the cell is the best place to calculate its own height. The pattern I use is to implement a class method on the custom UITableViewCell class to calculate the cell's height given the data it is displaying. Call this from your implementation of tableView:heightForRowAtIndexPath:
I've never had an app rejected for variable height rows in either plain or grouped tables.

What replacement options are there for heightForRowAtIndexPath in a UITableView?

tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath is deprecated according to the iOS docs. It mentions using rowHeight instead, but that property is for all rows. I want to flow the rows' height versus the content's (UILabel) varying size.
Is several custom cell styes my only other option besides heightForRowAtIndexPath?
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html
Sorry to tell you that...but it isn't deprecated ;)
EDIT: only thing they are saying is if you have very big tables (1000+ cells) it's better to use rowHeight because of performance issues.

Resources