从UITableViewCell刷新UITableView

Arr*_*rel 7 iphone cocoa-touch uitableview

有没有一种简单的方法可以从UITableViewCell中触发[UITableView reloadData]?我正在加载要在初始表格显示后显示的远程图像,并且更新图像时self.image = newImage不刷新表格.重置单元格的文本值会刷新表格,但这似乎很草率.

MyTableViewCell.h

@interface MyTableViewCell : UITableViewCell {}
- (void)imageWasLoaded:(ImageData *) newImage;
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

bbr*_*own 18

我做了你想要做的确切事情.你要找的东西是needsLayout.即(这是我的UITableViewCell子类上的通知观察者):

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

这将弹出您的图像,而无需重新加载整个表,这可能会非常昂贵.


Cod*_*rue 5

UITableView使用superview属性获取对包含的引用.然后告诉它" reloadData":

UITableView *parentTable = (UITableView *)self.superview;
[parentTable reloadData];
Run Code Online (Sandbox Code Playgroud)