font in tableviews

Discussion in 'iOS Programming' started by LARRYSE, Nov 11, 2012.

  1. LARRYSE macrumors newbie

    Joined:
    Aug 22, 2012
    #1
    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?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    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... ;)
     
  5. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #5
    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.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    @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.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Understood. Thanks for clarifying.
     

Share This Page