我想在表格视图中为每一行下载图像,所以我编写了一个方法来通过使用块来完成此操作。什么是嵌套块中保留对象的正确方法
我认为indexPath和tableView可能不会被完成块复制,因为它们在If语句中使用。所以我在完成块执行前保留它们。
而且代码:
- (void)downloadImageAtURL:(NSString *)imageURL name:(NSString *)name serial:(BOOL)serial forTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *cachedImagesOfURLs = self.cachedImagesOfURLs;
UITableView *strongTableView = [tableView retain];
NSIndexPath *strongIndexPath = [indexPath retain];
[self.downloadManager downloadImageAtURL:imageURL
identifier:[self identifierForIndexPath:indexPath]
serial:serial
completion:^(UIImage *image) {
if (image) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[cachedImagesOfURLs setObject:image forKey:imageURL];
id cell = [strongTableView cellForRowAtIndexPath:strongIndexPath];
if ([cell respondsToSelector:@selector(didDownloadImage:withName:)]) {
[cell didDownloadImage:image withName:name];
}
}];
}
[strongTableView release];
[strongIndexPath release];
}];
}
但结果是,当完成块EXCUTE并尝试创建一个块和主线程中运行它,它崩溃。调试器打印“- [XXX cellForRowAtIndexPath:]:无法识别的选择器发送到实例”。我似乎tableView和indexPath被释放。
但我不知道为什么,我试图挽留他们。有人能告诉我如何防止这种事故发生?非常感谢你!
尝试self.tableView – 2013-03-14 04:17:11
不过是要保留这一点,需要你有没有尝试过的方法?变量直接在块?我认为它可能工作 – 2013-03-14 04:22:43
块复制它需要的变量。不需要明确保留。只要避免在块中提及self.downloadManager。 – danh 2013-03-14 04:24:28