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
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.
Get a reference to the containing UITableView
using the superview
property. Then tell it to "reloadData
":
UITableView *parentTable = (UITableView *)self.superview;
[parentTable reloadData];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With