Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

LARRYSE

macrumors newbie
Original poster
Aug 22, 2012
12
0
Hello,
I have a tableView where I set the cell height dynamically with

Code:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *itemToSize = [[NSMutableArray alloc] init ];    
    [itemToSize addObject:cat.question];
    [itemToSize addObject:cat.answer];    
    NSString *item = [itemToSize objectAtIndex:indexPath.row];
    
    CGSize size = [ item
                   sizeWithFont:[UIFont systemFontOfSize:17]
                   constrainedToSize:CGSizeMake(200,CGFLOAT_MAX)];
    
    return size.height + 12;
}

Now if I put the font line to change the font in the cellForRowAtIndex method it messes up the some cells with lots of extra space because the heightForRow method is not using the new font type. How can I size cells using the non default font?

Can I set the font in the heightForRowAtIndexPath?
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Now if I put the font line to change the font in the cellForRowAtIndex method it messes up the some cells with lots of extra space because the heightForRow method is not using the new font type. How can I size cells using the non default font?

Can I set the font in the heightForRowAtIndexPath?

I wouldn't recommend setting the font in heightForRowAtIndexPath, since you have no reference to the cell at that point, and looking one up could be costly. Instead, I suggest having the cellForRowAtIndexPath calculating and storing the desired row height and then having heightForRowAtIndexPath simply look that value up.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
There are various ways to do this. You could store the font or the font size in your data model. Then look it up in heightForRowAtIndexPath and use it.

This whole business with heightForRowAtIndexPath is always difficult.
 

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
The problem is timing.
heightForRowAtIndex is called on every index before cellForRowAtIndex is even called to create the first cell. Just after numberOfRowsInSection. Plus the tableview will do a full sanity check of the table data including cell heights every time you do any updates as well. It needs that number early and often if you update your tables much.

Sure keep them out of the datasource but you need to be able to derive the height direct from the datasource to get it work.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
@dejo, if there's some other heuristic to decide what font goes with what row that's fine. It doesn't have to be in the data model.

The data models that I refer to exist only in the view controller. It can include a string for the row, images for the row, and other info that needs to be set in cellForRowAtIndexPath. But not every part of that would be saved permanently. The point is to calculate all the info that will be displayed one time and then save it in the view controller's data model.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
The data models that I refer to exist only in the view controller. It can include a string for the row, images for the row, and other info that needs to be set in cellForRowAtIndexPath. But not every part of that would be saved permanently. The point is to calculate all the info that will be displayed one time and then save it in the view controller's data model.

Understood. Thanks for clarifying.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.