I am developing an app with an adaptive layout and everything works so far except I need to have adaptive table view cell heights. I am using a storyboard and do not see any way to do this. Is it mission impossible?
-
Yes it's possible, you need to define the default constaints for Any/Any classes and then define your custom constraints for the specific size classes – Joël Salamin Mar 16 '15 at 21:53
-
As far as I can see the custom constraints are restricted to the width and height of individual rows in the cell and not to the overall height of the cell. In my case I want to be able to have a larger font size on the iPad but if I size the row cell to support that then the display has too much white space on an iPhone. – PatriciaW Mar 17 '15 at 13:43
-
Despite my answer, I think there is a way you can do this in IB. You can have different views appear in different size classes. So you could have a label with one font size for one size class, and a different label with a larger font size for a larger size class. – rdelmar May 08 '15 at 19:29
-
I don't understand. I want the height of individual Table View Cells to depend upon the size class. I can change the height of an embedded Text View for different size classes and also the font size. But changing the font size means that the Text View height changes but not the parent Table View Cell. So there is empty space in different size classes. – PatriciaW May 09 '15 at 15:17
-
This question doesn't deserve a down vote. Questions related to new and complex topics should be understood properly and answered rather than demotivating the person by giving him/her vote down instead of answer. – Developer Jun 24 '16 at 09:44
1 Answers
If I'm understanding your question correctly, you want to have your cell height adjust to different font sizes using size classes; so have a bigger font on a bigger size screen that will cause the cells to adjust accordingly. I think the only way to do this is to have different text views installed for particular size classes.
I made a test app that demonstrates this. In the storyboard, I added a table view controller with a custom cell. I changed the size class to wCompact hAny
and added a text view (IBOutlet: labelPhone) to the cell. I pinned it to all 4 sides of the cell, disabled scrolling, made the text red, and sized the font to 8. I changed the size class to wRegular hAny
, and added another text view (IBOutlet: labelPad) to the cell with the same constraints and with green text of font size 30. The code in the table view controller was as follows,
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 50;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.tableView reloadData]; // this was necessary to have the cells size correctly when the table view first appeared
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *theText = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt";
cell.labelPhone.text = theText;
cell.labelPad.text = theText;
return cell;
}
When run on the iPhone simulator, I got small red text, and when run on the iPad simulator, I got large green text. I put the sample app here, http://jmp.sh/WfeZ8Pi
After Edit:
Actually, I found an easier way. You can add your subview (text view or label) using the default wAny hAny
size class (you don't need two different views). In the Attributes Inspector, next to the Font field, there's a little "+" button. Click on that, and you can add different font sizes for different size classes. That's all you need to do. Then cellForRowAtIndexPath becomes something like this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textView.text = self.theData[indexPath.row];
return cell;
}

- 103,982
- 12
- 207
- 218
-
Thank you very much for the clear example. I now understand what @dasblinkenlight was explaining. Thanks to you both. The only problem remaining for me is that I generate the text dynamically so that each cell on a view has a different height. Do you think I can modify your script to change the height based on the length of the text string? As I said below I am still very much of a rookie in this area. – PatriciaW May 12 '15 at 15:10
-
@PatriciaW I'm using self-sizing cells, so this should work with varying string length without modification. You just need to replace the static string with something like `NSString *theText = myArray[indexPath.row];` Because the text view is tied to the top and bottom of the cell, the cell's height will adjust to the intrinsic height of the text view. – rdelmar May 12 '15 at 15:54
-
@PatriciaW I found an easier way to use different fonts based on the size class. I've updated my answer to include that. – rdelmar May 13 '15 at 01:33
-
I was already using the font size for size classes. That part was working OK ... it is just the height of the cell that is the problem. I'll see if I can use your suggested code. Thanks. – PatriciaW May 22 '15 at 14:16
-
I'm still stuck .. probably at the conceptual level. Here's the problem (as I see it). I have many tableviewcells on a screen and each cell is created in a custom view controller script. – PatriciaW May 23 '15 at 18:14
-
Got cut off editing the above comment. I'm still stuck .. probably at the conceptual level. Here's the problem (as I see it). I have many tableviewcells on a screen and each cell is created from core data in a custom viewcontroller script. Would it be better to create a custom tableviewcell script? – PatriciaW May 23 '15 at 18:20
-
@PatriciaW I don't know what you mean by "custom view controller script". The code I posted does change the cell height based on the text view's height which in turn is based on the font size. If your cells aren't adjusting their height correctly, then you should ask a new question, and post the code you're using for your table view controller. – rdelmar May 23 '15 at 18:30
-
I was a bit sloppy with my terminology. I meant a custom class for the UITableViewController. I haven't been able to implement your code because among other things I didn't know how to determine the indexPath and associated it with the textViews. Perhaps it would be better to post the code in a new question. Thanks. – PatriciaW May 23 '15 at 20:32
-
@PatriciaW You don't need to associate the indexPath with a text view (and in fact you shouldn't since cells are reused). You definitely should ask a new question and show your code, because it's not clear what your problem is. – rdelmar May 23 '15 at 21:44
-
@rdelmar I have posted a new question and code at http://stackoverflow.com/questions/30424623/different-table-view-cell-row-heights-for-different-size-classes – PatriciaW May 24 '15 at 14:31
-
What is "theData" in your sample code? (This is one source of my confusion) – PatriciaW May 24 '15 at 17:32
-
@PatriciaW theData is the array of strings that I use to populate the cells. – rdelmar May 24 '15 at 17:35
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78649/discussion-between-patriciaw-and-rdelmar). – PatriciaW May 24 '15 at 19:53