views:

2806

answers:

3

I have a UITableView with a custom UITableViewCell. In cellForRowAtIndexPath, I assign a font to a label in every cell, since the user can change font size at any time. To change font size, the user clicks a button below the tableview, which dislays another view with settings. Once they have chosen a font size and click done, that view goes away and the tableview displays again. I display one cell per viewing area. So the user doesn't see the font change until he scrolls to the next cell. The current cell is the one I'd like to update.

I've tried reloadData from the settings screen but that didn't work. The tableview is a UITableViewController but viewWillAppear never fires once the settings screen goes away. I've tried making the custom cell a property of the tableview so it can be accessed from the settings view, then called setNeedsDisplay and setNeedsLayout. Those didn't work either. Any suggestions?

+1  A: 

For the cells that are still there you have to update the font of the label. You'll need a way to access the custom label in your custom cell.

After changing the font size do something like:

for (MyCell* cell in [theTableView visibleCells]) {
    [cell.myLabel.font = newFont];
}
Nikolai Ruhe
I tried your code in the settings view but still no change on the current cell once the tableview displays. If all I do is call reloadData on the tableview from the settings view, it works fine. However, that seems like a very expensive, overkill way to do it.
4thSpace
I edited my answer after better understanding the problem.
Nikolai Ruhe
A: 

I'm curious, how do you display your settings screen? viewWillAppear should be called under normal circumstances when returning. Do you use

[self presentModelViewController:animated:]

or

[self.navigationController pushViewController:animated:]

These should both work. If, on the other hand, you're just adding the settings view as a subview of the window or current view or something, then removing it won't call viewWillAppear on the table view controller.

So if you're not using one of those two methods, do. If you already are, then setting the font of the cell is really as simple as getting the cell and setting its font. If you're using a standard UITableViewCell, that's done with

cell.font = newFont;

If you're using a custom subclass, I obviously can't tell you exactly how it needs to work, but the way Nikolai has it is correct.

Ed Marty
I am using presentModalViewController. The solution below worked fine.
4thSpace
+2  A: 

In your custom UITableViewCell, call [self setNeedsLayout]; and it should repaint your cell at the next loop. I use it for asynchronous image loading since I'm pulling the image from the Web and it works swell.

bbrown