Reputation: 31
I have a UITableView
with custom cells. I need to display Title, Address, Zip, Phone, Email, Website and Description in a Cell. All this information is coming from webserver. I am able to display the contents from the webserver. However, if any of the content is empty, there is a gap where that content should be, and if description is too long I am not able to display all the content. How can I change the height of cell according to the content from server? Please help.
For example : the contents is printing like: Title Address Zip Phone...
but, if Address is nil then it looks like: Title
Zip Phone...
I have the tableview:heightForRowAtIndexPath
method but I am not able to update the cell height according to the cell contents.
Sorry for bad question format
Upvotes: 0
Views: 167
Reputation: 21
Cells resizing can get pretty complicated, so I suggest you simply use a table view framework such as "Sensible TableView", where all the cells are automatically resized to fit contents. I believe they also now have a free version.
Upvotes: 2
Reputation: 1213
Your tableView:heightForRowAtIndexPath: call will need to tabe into account the missing data and how all the rest of the cell will be re-laid out to close the gap it leaves. So, your actual call will be non-trivial here.
The new facebook app does a neat trick here, though, to save you calling it every time. It calculates all of the cell heights in the background, just after downloading it, and before rendering the content to the user, storing it in the data store alongside the data itself. This means that the actual table cell rendering is really fast and slick as you don't need to re-layout/recalculate the cell height and layout on each app run/cell reuse.
Upvotes: 0
Reputation: 1294
Firstly you need to set the size of your labels according to it's text length. There are a number of sizeWithFont
methods available to get size for a string. See Apple Developer Documentation
If you have single line labels you can use simplest of them – sizeWithFont:
. But if you have multiline labels you should use – sizeWithFont:constrainedToSize:
. This method lets you specify the maximum size you want the label be.
Secondly you have to return calculated height for your cell using tableView:heightForRowAtIndexPath:
Hope this helps you.
Upvotes: 0
Reputation: 20551
you can set the height of every cell with this delegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [yourArray objectAtIndex:indexPath.row];// here just use your data array which you get from server
CGSize mTempSize = [text sizeWithFont:[UIFont fontWithName:fontName size:fontSize] constrainedToSize:constrainedToSize lineBreakMode:UILineBreakModeWordWrap];
return mTempSize.height ;
}
i hope this help you...
Upvotes: 0