My goal is to set the text, then make an asynchronous call for image, and set the image on arrival.
I currently make an asynchronous call for image to set in UITableViewCell
, and noticed my previous technique resulted in a loop I recently identified:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:@"id"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"]; } [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:userAvatar]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (data) { UIImage* avatarImage = [UIImage imageWithData:data]; if (avatarImage) { dispatch_async(dispatch_get_main_queue(), ^{ [cell.imageView setImage:avatarImage]; }); /* .--. [STOP] .----' '--. | '-()-----()-' | If I include the following, the program goes in a loop, and have not readily identified workaround: dispatch_async(dispatch_get_main_queue(), ^{ [_tableView reloadData]; }); [GO] .--. | .----' '--. | '-()-----()-' */ } } }]; cell.textLabel.text = @"foo"; cell.detailTextLabel.text = @"bar"; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; }
Is there a methodology to produce the outcome I am looking for without a custom setter in custom class?
reloadData
fromcellForRow...
. Bad things happen. Look at Apple'sLazyTableImages
sample app to see how to do this properly. – rmaddy