Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh UITableViewCell?

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?

like image 584
4thSpace Avatar asked Jun 04 '09 14:06

4thSpace


People also ask

How to refresh tableview cell?

You can call [self. tableview reloadData]; on the tableview and refresh the entire table. Otherwise you can do this to refresh a single cell.

How do you refresh a cell in Swift?

Reloading a cell/row at an IndexPath is easy and all you need to use is the reloadRows(at:, with:) .

How to reload UITableView?

If you want to reload your table view while also saving and restoring any selections, you should take a copy of the indexPathsForSelectedRows property before the reload, then re-apply those selections after calling reloadData() . With that in place, you can now call yourTableView.


2 Answers

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.

like image 101
bbrown Avatar answered Oct 22 '22 14:10

bbrown


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];
}
like image 41
Nikolai Ruhe Avatar answered Oct 22 '22 14:10

Nikolai Ruhe