NSThread,UITableViewCell和Image

| 我的UITableView有问题。我将像往常一样下载数据并设置单元格,但是这次添加了NSThread。 首先,我会在视图加载后调用ASIHTTPRequest 然后在-(void)requestFinished:(ASIHTTPRequest *)request中,我将缩略图的网址添加到NSMutableArray中
 NSString *photoURLString = [NSString stringWithFormat:@\"http://some.url.com/img/%@\",[thearray objectForKey:@\"tn_url\"]];    
[thumbNailURL addObject:(photoURLString)];
在cellForRowAtIndexPath中:我要设置要显示的其他数据,我将调用我的NSThread
[NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:indexPath];
在我的loadImage线程中
- (void) loadImage: (NSIndexPath*) indexPath{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *iconURL = [thumbNailURL objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:iconURL]; 
UIImage *icon = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
cellImageView = [[UIImageView alloc] initWithFrame:CGRectMake(11.0, 6.0, 61.0, 50.0)];
[cellImageView setImage:icon];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell.contentView performSelectorOnMainThread:@selector(addSubview:) withObject:cellImageView waitUntilDone:NO];
[pool drain];
} 加载的所有数据和图像都很好,但是如果我上下滚动得更快,该应用程序将因以下错误而挂起
[1997:7a1f] *** Terminating app due to uncaught exception \'NSRangeException\', reason: \'*** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 5]\'    (0x31312d2f 0x345f50e3 0x3127d1b1 0x35aefc1d 0xf4dd 0x333ffb51 0x3347c67b 0x366af589 0x366b2310) terminate called throwing an exceptionProgram received signal:  “SIGABRT”.
如果我错了,如果有人可以向我展示正确的配置,那就太好了,因为过去三天​​我一直在尝试不同的方法。     
已邀请:
        几件事。 首先,我建议您考虑使用SDWebImage之类的东西进行异步图像下载(看起来Olivier Poitrey是原始作者)。 Github存储库在这里:https://github.com/rs/SDWebImage。我将其用于此目的,这很棒。 也就是说,我在这里看到一些东西。 1)您似乎并没有在检查是否已设置单元格图像...如果已经安装,则无需重新下载图像。因为
dataWithContentsOfUrl
可能提供一些缓存,所以您可能会避免这样做,但是我不确定。 2)我无法从此处的代码中分辨出,但是您需要确保在调用
requestFinished
之后才允许表视图加载其数据。否则,您将遇到竞争状况,这可能会导致您看到的错误类型。考虑在
requestFinished
方法中设置tableView的数据源,然后调用reloadData。否则,请按照建议的方式进行操作,并仅在URL数组中填充
[NSNull null]
个对象。     
        您是否尝试过让数组\“ thumbNailURL \”包含[NSNull null]对象来表示要显示的项目数,然后在异步加载它们后将其替换,那样您就无法比该数组快。
thumbNailURL = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [items count]; i++) {
    [thumbNailURL addObject:[NSNull null]];
}
然后简单地:
[thumbNailURL replaceObjectAtIndex:index withObject:thumbnail];
    

要回复问题请先登录注册