Is my - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath delegate I have the following code:
if ([movie isDownloaded])
cell.detailTextLabel.text = movie.duration;
else
{
cell.detailTextLabel.text = @"";
[movie downloadInQueue:self.downloadQueue completion:^(BOOL success) {
UITableViewCell *updateCell = [tblView cellForRowAtIndexPath:indexPath];
if (updateCell)
{
updateCell.detailTextLabel.text = movie.duration;
[updateCell setNeedsLayout];
}
}];
}
Which calls into Movie.m and runs this code:
- (void)downloadInQueue:(NSOperationQueue *)queue completion:(void (^)(BOOL success))completion
{
if (!self.isDownloading)
{
self.downloading = YES;
[queue addOperationWithBlock:^{
BOOL success = NO;
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:self.fileURL];
CMTime timeduration = playerItem.duration;
float seconds = CMTimeGetSeconds(timeduration);
self.duration = [self timeFormatted:seconds];
self.downloading = NO;
self.downloaded = YES;
success = YES;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completion(success);
}];
}];
}
}
When my cells become not visible, I want to cancel the NSOperation in the Movie object if it hasn’t been run yet (remove it from the queue). I know I can subclass UITableViewCell and do something like this:
- (void)willMoveToWindow:(UIWindow *)newWindow
{
[super willMoveToWindow:newWindow];
if (newWindow==nil) {
// Cell is no longer in window so cancel from queue
}
}
Question… how can I cancel my Movie NSOperation from within the UITableViewCell delegate call? With a delegate or NSNotification of some kind? I need to know the indexPath of the cell to get the correct Movie object out of my array and cancel the operation.
As of iOS 6 you can use the tableView:didEndDisplayingCell:forRowAtIndexPath: method of the UITableView Delegate protocol. This gets called when the cell is removed from the table view (which happens when it is no longer visible).