2
votes

When I push a UIViewController onto my UINavigation controller like:

[(UINavigationController *)self.parentViewController pushViewController:[[[Fonts alloc] initWithNibName:@"Fonts" bundle:nil] autorelease] animated:YES];

Where Fonts.xib is a UIView with only UITableView controlled by a Fonts object that is a subclass of UIViewController and acts as the UITableView's dataSource and delegate.

In the Fonts object I create a UITableViewCell like:

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"BlahTableViewCell"];

    if (!cell) {
        cell = [[UITableViewCell alloc]
                initWithStyle: UITableViewCellStyleDefault
                reuseIdentifier: @"BlahTableViewCell"];
        [cell autorelease];  // Delete for ARC
    }

    return cell;

}

And then I change the font of the cell here:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell.textLabel setFont:[(UIFont *)[self.listOfFonts objectAtIndex:indexPath.row] fontWithSize:cell.textLabel.font.pointSize]];
    cell.textLabel.text = [(UIFont *)[self.listOfFonts objectAtIndex:indexPath.row] fontName];
}

listOfFonts is an NSArray of UIFont objects.

When the view appears it looks like UITableView without changed fonts

If I call reloadData on the UITableView or if I drag the UITableViewCells off screen with my finger and let them bounce back they are redrawn and the view the cells display with the labels having their fonts changed.

It seems like the issue is the UITableViewCells are being drawn too early. If I delay the drawing of them everything looks correct but I want the UITableView to be displaying correctly when the UINavigationController slides my view into place.

Any idea what I am doing wrong?

EDIT: I uploaded a simple and straightforward example of my issue to Dropbox. http://dl.dropbox.com/u/5535847/UITableViewIssue.zip

3
Why don't do this in cellForRow method??? - Roman Temchenko
@RomanTemchenko UI changes (e.g. background colours & fonts) on table cells is supposed to be done in tableView:willDisplayCell: - Ell Neal
please see my comment below for the proper answer. - user537213

3 Answers

4
votes

SOLVED IT!

Ok so I was having exactly the same issues as the original poster and this was the problem.

The line that's causing issues is:

[cell.textLabel setFont:[(UIFont *)[self.listOfFonts objectAtIndex:indexPath.row] fontWithSize:cell.textLabel.font.pointSize]];

Specifically, your issue is because you're trying to feed the cell's textLabel its own pointSize, but pointSize doesn't exist yet so strange bugs occur instead. For me, I noticed that a "transform" was failing due to a singular matrix being non-invertible. As soon as I hardcoded a standard value as my pointSize I saw all my labels draw with the proper font instantly. Note: this makes sense as to why a redraw worked, because then your textLabel does indeed have a pointSize.

In any case, you need to explicitly set your pointSize here, no using what the textLabel "already has" because it doesn't have anything until you're "reloading" a cell.

2
votes

Set the label font inside -tableView:cellForRowAtIndexPath:.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *identifier = @"identifier";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];

    // do it here if your font doesn't change ....
  }

  // otherwise here with your font ...
  cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
  return cell;

}

0
votes

I'm not sure that table cells are designed to be customisable in this way. The table cell may assume that you won't customise the font, and so not draw itself in a way that's compatible with what you are trying to do.

You'd be better off creating a custom table cell, or appending a UILabel as a subview to the table cell when you create it, and them setting the font of that label instead.

It may seem like overkill for such a small customisation, but it's flexible and it's guaranteed to work.