Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing the UITableView from UITableViewCell

Is there an easy way to trigger a [UITableView reloadData] from within a UITableViewCell? I am loading remote images to be displayed after the initial table display, and updating the image with self.image = newImage does not refresh the table. Resetting the cell's text value does refresh the table, but this seems sloppy.

MyTableViewCell.h

@interface MyTableViewCell : UITableViewCell {}
- (void)imageWasLoaded:(ImageData *) newImage;

MyTableViewCell.m

@implementation MyTableViewCell
- (void)imageWasLoaded:(UIImage *) newImageData {
    self.image = newImage; //does not refresh table

    //would like to call [self.tableView reloadData] here,
    //but self.tableView does not exist.

    //instead I use the below line
    self.text = self.text; //does refresh table
}
@end
like image 288
Arrel Avatar asked Jun 25 '09 01:06

Arrel


2 Answers

I did the exact thing that you're trying to do. The thing you're looking for is needsLayout. To wit (this is a notification observer on my UITableViewCell subclass):

- (void)reloadImage:(NSNotification *)notification
{
    UIImage *image = [[SSImageManager sharedImageManager] getImage:[[notification userInfo] objectForKey:@"imageUrl"];
    [self setImage:image];
    [self setNeedsLayout];
}

This will pop in your image without having to reload the entire table, which can get very expensive.

like image 188
bbrown Avatar answered Sep 21 '22 16:09

bbrown


Get a reference to the containing UITableView using the superview property. Then tell it to "reloadData":

UITableView *parentTable = (UITableView *)self.superview;
[parentTable reloadData];
like image 43
CodeGrue Avatar answered Sep 22 '22 16:09

CodeGrue