Update UITableView from UITableViewCell

Is there an easy way to call [UITableView reloadData] from a UITableViewCell? I upload deleted images that will be displayed after the initial display of the table, and updating the image with self.image = newImage does not update the table. Resetting the text value of a cell updates the table, but it seems messy.

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 
+6
iphone cocoa-touch uitableview
source share
7 answers

I did what you are trying to do. The thing you're looking for is needsLayout . For this (this is the notification observer in my subclass of UITableViewCell):

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

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

+18
source share

Get a link to the containing UITableView using the superview property. Then say " reloadData ":

 UITableView *parentTable = (UITableView *)self.superview; [parentTable reloadData]; 
+5
source share

Rebooting partitions / lines requiring a reboot is less expensive. IPhone SDK 3.0 has several ways to do just that.

0
source share

Hm. So, imageWasLoaded: is the delegate method called by some asynchronous image loading code? It actually seems strange that setting self.image does not update the image. Have you tried adding your own UIImageView to the cell, rather than using the image property? Perhaps this will be a hack, but in this way the image should be updated immediately (without having to reload the entire table view, which is definitely undesirable).

Even better: if you are using the SDK 3.0, you can use the new imageView property in the UITableViewCell (although I haven't tried it yet):

 self.imageView.image = newImage; 

Actually, I needed to do the same a few weeks ago. However, my approach was to subclass UIImageView and do all the asynchronous upload / update of the image in this class. The main reason I did this was because I wanted to have a common and reusable component that could be used in different cells of the table view (or elsewhere).

0
source share

CodeGrue is right. After several days thinking why [subclassOjb.tableView reloadData] does not work, I gave up trying to understand this and decided to use only two methods that I know, and it is very easy to implement: 1- Notification Center (from tableviewcell you should not use this one) 2- or use a supervisor to get a handle to your tableview property (this is ideal for uitableviewcells).

Please note that using the supervisor method will only work on your uitableviewcell class, most likely, if you have a custom cell, you will have a uitableviewcell class, the supervision of which will be your table controller or any other class that you have with the uitview + delegate datasource.

Now, if you need to reload the table view from another class, let the datasource class say singleton or something else; you must use a block in the tableview class to load data in another thread with an internal block to reload the table on mainthread.

 //handle tablview to show spinner (you have to lower your tableview out of the way yourself if you are calling this method) [self.tableView setContentOffset:CGPointMake(0, -rf.frame.size.height) animated:YES]; //start spinner animation here [rf beginRefreshing]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) { // Load Data from data source class here [myDataSourceClass fetchDataMethod]; dispatch_async(dispatch_get_main_queue(), ^{ //Reload data and stop spinner [self.tableView reloadData]; [rf endRefreshing]; //handle tablview to hide spinner atrributed title [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES]; }); }); 

If you are at a different level and something happens, you need to reload the table in your table view. Well, at this moment your table is not on the screen, there is no reason why you should reload the table if no one sees it. Just add [self.tableview reloadDAta] to your viewWillAppear method, when users return to the viewviewcontroller, it will reload the table on the fly.

In any case, for tableviewcell, just use the following inside your action to reload Data (credit for CodeGrue)

 UITableView *parentTable = (UITableView *)self.superview; [parentTable reloadData]; 

** Another topic, but for those of you who are starting now. This will save you from possible headaches - after creating your tableclass and before you even begin to deal with its delegation methods - do the following: in your DidLoad view, add the following code:

 self.tableView.delegate = self; self.tableView.dataSource = self; 
0
source share

Anyone looking for a way to reload a table from a cell and realized that CodeGrue's answer is not working, you can still do this, but you need to double check.

Renouncement

You should always use a delegate template for this, which follows a hack that may break again in future versions.

Hack reloading data from cell

 UITableView *parentTable = (UITableView *)self.superview; if (![parentTable isKindOfClass:[UITableView class]]) { parentTable = (UITableView *) parentTable.superview; } [parentTable reloadData]; 
0
source share

try [yourtableview reloadData]; after installing the image on a new image

-one
source share

All Articles