Custom UITableViewCell - Asynchronous UIImage for UIImageView

I have created a custom UITableViewCell which is composed by 2 UILabels and a single UIImageView.

Data associated with cells is available with a NSObject class named CellInfo. CellInfo has 2 properties of NSString type and an UIImage property.

When I create a CellInfo instance, inside the initWithData method (CellInfo class), I do the following:

if(self = [super alloc])
  //initialize strings variables = aName;
  self.descritpion = aDescription;
  [self grabImage]    
return self;

where grabImage (within CellInfo class) using ASIHTTPrequest framework to grab images in asynchronous manner (in the following code NSURL is alaways the same but in reality it changes with data)

- (void)grabImage
   NSURL *url = [NSURL URLWithString:@""];
   __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

   [request setCompletionBlock:^{               

      NSData *data = [request responseData];
      UIImage* img = [[UIImage alloc] initWithData:data];

      self.image = img;
      [img release];

      // Send a notification if image has been downloaded
      [[NSNotificationCenter defaultCenter] postNotificationName:@"imageupdated" object:self];
   [request setFailedBlock:^{
      NSError *error = [request error];
     // Set default image to self.image property of CellInfo class
   [request startAsynchronous];

I have also a UITableViewController that loads data into the custom cell like the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    // Do stuff here...

    // Configure the cell...
    ((CustomTableViewCell*)cell).nameOutlet.text = ((CellInfo*) [self.infoArray objectAtIndex:indexPath.row]).name;
    ((CustomTableViewCell*)cell).descriptionOutlet.text = ((CellInfo*) [self.infoArray objectAtIndex:indexPath.row]).descritpion;
    ((CustomTableViewCell*)cell).imageViewOutlet.image = ((CellInfo*) [self.infoArray objectAtIndex:indexPath.row]).image;

    return cell;

In addiction, this UITableViewController observes notification from the CellInfo class because, at start up, images for visible cells are not displayed. This is the method that is called when the notification is captured:

- (void)imageUpdated:(NSNotification *)notif {

    CellInfo * cInfo = [notif object];
    int row = [self.infoArray indexOfObject:cInfo];
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:0];

    NSLog(@"Image for row %d updated!", row);

    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationNone];

The code works well, but I would like to know if I'm doing right or there is a better way to do this. My doubt is the following: is it correct to save downloaded images within each CellInfo instance or is it possible to follow another way to cache images using, for example, cache policy provided by ASIHTTPRequest?

P.S. grabImage is not called if the image for a specific CellInfo instance has already been downloaded.


I believe that's pretty neat. Instead of that you might subclass UIImageView class and create an initializer like [AsyncUIImageView initWithURL:] and then put that ASIHttpRequest logic inside the view.

After it finishes loading the picture, there could be two ways:

  1. It can call [self setNeedsDisplay] (an UIView method) so image view is redrawn.

  2. You can pass UITableViewCell or UITableView as a delegate to AsyncUIImgeView so that it could tell table view to reload that cell.

Need Your Help

Chunk-encoded HTTP response before POST HTTP chunk-encoded request complete

java http chunked-encoding

I was wondering if it is possible to begin a chunk-encoded HTTP response while still receiving an HTTP request which is also chunk-encoded?

how to use TCL packages/code in python pytest program

python tcl py.test

I have been using TCL code, but i want use my all TCL packages/code in python pytest. Is it possible? If yes please let me know the process to accomplish this task.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.