Reputation: 1681
I'm trying to dynamically adjust the height of a UITableView
cell using the code below. At the moment only the return 200;
is being called.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *description = [photosFromCommentsArray objectAtIndex:indexPath.row];
NSLog(@"description is %@",description);
if(description == NULL)
return 60;
else {
return 200;
}
}
the NSLog
output for the description is:
as you can see, sometimes it's in which case I would like to return return 60;
and when it contains something other than I would like it to return return 200;
2013-09-12 16:30:36.567 1000 [11619:907] description is <null>
2013-09-12 16:30:36.568 1000 [11619:907] description is <null>
2013-09-12 16:30:36.569 1000 [11619:907] description is <null>
2013-09-12 16:30:36.569 1000 [11619:907] description is <null>
2013-09-12 16:30:36.570 1000 [11619:907] description is (
"http://****.net/uploads/nodes/1464/photos/a0d41e7834b5119884c6fa83aaffb3023f609c0e.jpeg"
)
2013-09-12 16:30:36.571 1000 [11619:907] description is (
"http://****.net/uploads/nodes/1464/photos/739701d7609bcb65ce5293f3551773c6bb6a498d.jpeg"
)
2013-09-12 16:30:36.572 1000 [11619:907] description is (
"http://****.net/uploads/nodes/1464/photos/99cf9e662b0d4f00e316bd2f8c01faddb20e9473.jpeg"
)
2013-09-12 16:30:36.572 1000 [11619:907] description is (
"http://*****.net/uploads/nodes/1464/photos/a6680113da9567b8477810703e14616631c39a06.jpeg"
)
Upvotes: 0
Views: 285
Reputation: 2085
With in the delegate method tableView:heightForRowAtIndexPath: you return the height of the row, from there you can adjust the height of the cell to your needed dimension to accommodate for text, images, etc.
From my experience the best way to do this is
NSString* description = [photosFromCommentsArray objectAtIndex:indexPath.row];
UITextView* dummyTextView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,width_Of_Text_With_In_Cell, 1];
[dummyTextView sizeToFit];
return defaultHeight + dummyTextView.frame.size.height;
now this looks weird and questionable, but when i tried to get the exact height of the text, the values wouldn't come out very well. Using the textview it gave me a perfect size every single time. When using
[string sizeWithFont:constrainedToSize:lineBreakMode:];
the height and width would be different depending on how the text was rendered, which makes sense, different letters have different widths and different heights, sometimes the text would drop to the next line, sometimes it wouldn't, and the calculations for the height would be off sometimes.... and it gave me an overall hassle.
This code works if you content is in a UITextView, resizing labels or textfields would add to the hassle above..., so make sure your description is a textView with in the cell.
Upvotes: 0
Reputation: 1818
You didn't show how photosFromCommentsArray
is being initialised. So, try this:
if(description == (id)[NSNull null])
return 60;
else
return 200;
Or:
if(description == nil)
return 60;
else
return 200;
Upvotes: 2
Reputation: 5681
You can simply return the height of the string using:
float height = [description sizeWithFont:[UIFont YOURFONT] constrainedToSize:CGSizeMake(tableView.frame.size.width, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping].height;
This will return the height of the string when it is constrained to the width of the tableview and in a particular font.
You may want to check that it is a minimum height incase you have some cells with very little text.
Upvotes: 0