Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 11, 2012, 06:26 PM   #1
LARRYSE
macrumors newbie
 
Join Date: Aug 2012
font in tableviews

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?
LARRYSE is offline   0 Reply With Quote
Old Nov 12, 2012, 09:09 AM   #2
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by LARRYSE View Post
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.
__________________
dejo is offline   0 Reply With Quote
Old Nov 12, 2012, 06:05 PM   #3
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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.
PhoneyDeveloper is offline   0 Reply With Quote
Old Nov 12, 2012, 06:16 PM   #4
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by PhoneyDeveloper View Post
You could store the font or the font size in your data model.
Normally, I wouldn't recommend this. Best to keep your data model and UI needs as separate as possible. Kinda the MVC mantra. Of course, if you wanted to integrate a UI model...
__________________
dejo is offline   0 Reply With Quote
Old Nov 12, 2012, 09:22 PM   #5
MattInOz
macrumors 68030
 
MattInOz's Avatar
 
Join Date: Jan 2006
Location: 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.
__________________
There is no such thing as "Collective Wisdom"
[ iPhone 5s, iPad Mini, 13" MacBookPro 2.7Ghz, 27"Al iMac i7, Black MacBook 13"]
MattInOz is offline   0 Reply With Quote
Old Nov 12, 2012, 10:20 PM   #6
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
@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.
PhoneyDeveloper is offline   0 Reply With Quote
Old Nov 13, 2012, 08:27 AM   #7
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by PhoneyDeveloper View Post
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.
__________________
dejo is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Pages: Pop up warning about missing font even after I've removed all of that font RandomHavoc Mac Applications and Mac App Store 4 May 10, 2014 12:02 AM
iPad: Multiple predicates in various tableviews jsanto iPad 0 Feb 11, 2013 04:16 PM
Mountain Lion changed font Rendering. System font is different. Can I change it? jaybar OS X 10.8 Mountain Lion 27 Aug 8, 2012 02:47 PM
Difference between a cell and a row in Tableviews? animefx iPhone/iPad Programming 4 Aug 7, 2012 02:35 PM
Two TableViews in a single ViewController ?? Fritzables iPhone/iPad Programming 2 Jul 8, 2012 07:25 PM

Forum Jump

All times are GMT -5. The time now is 09:06 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC