【发布时间】:2013-01-20 22:51:11
【问题描述】:
是我的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 代表我有以下代码:
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];
}
}];
}
调用 Movie.m 并运行此代码:
- (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);
}];
}];
}
}
当我的单元格变得不可见时,如果 Movie 对象中的 NSOperation 尚未运行,我想取消它(将其从队列中删除)。我知道我可以继承 UITableViewCell 并做这样的事情:
- (void)willMoveToWindow:(UIWindow *)newWindow
{
[super willMoveToWindow:newWindow];
if (newWindow==nil) {
// Cell is no longer in window so cancel from queue
}
}
问题...如何在 UITableViewCell 委托呼叫中取消我的 Movie NSOperation?有代表或某种NSNotification?我需要知道单元格的 indexPath 才能从我的数组中获取正确的 Movie 对象并取消操作。
【问题讨论】:
标签: ios multithreading cocoa-touch uitableview nsoperation