Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, don't queue operations while scrolling. Instead, load images for just the visible rows in <code>viewDidLoad</code> and when the user stops scrolling:</p> <pre><code>-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { for (NSIndexPath *indexPath in [self.tableView indexPathsForVisibleRows]) { [self loadImageForCellAtPath:indexPath]; } } </code></pre> <p>If you still want to be able to cancel loading for invisible cells, you could use <code>NSBlockOperation</code> instead of GCD:</p> <pre><code>self.operationQueue = [[[NSOperationQueue alloc] init] autorelease]; [self.operationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount]; // ... -(void)loadImageForCellAtPath:(NSIndexPath *)indexPath { __block NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ if (![operation isCancelled]) { NSString *galleryTinyImageUrl = [[self.smapi getImageUrls:imageId imageKey:imageKey] objectForKey:@"TinyURL"]; NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:galleryTinyImageUrl]]; dispatch_async(dispatch_get_main_queue(), ^{ if (imageData != nil) { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.imageView.image = [UIImage imageWithData:imageData]; } }); } }]; NSValue *nonRetainedOperation = [NSValue valueWithNonretainedObjectValue:operation]; [self.operations addObject:nonRetainedOperation forKey:indexPath]; [self.operationQueue addOperation:operation]; } </code></pre> <p>Here <code>operations</code> is an <code>NSMutableDictionary</code>. When you want to cancel an operation, you retrieve it by the cell's <code>indexPath</code>, cancel it, and remove it from the dictionary:</p> <pre><code>NSValue *operationHolder = [self.operations objectForKey:indexPath]; NSOperation *operation = [operationHolder nonretainedObjectValue]; [operation cancel]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload