【发布时间】:2019-09-08 04:30:54
【问题描述】:
我的 UITableView 有问题。我正在下载数据并像往常一样设置单元格,但这次添加了 NSThread。
首先我会在我的视图中调用 ASIHTTPRequest 并加载
然后在 - (void)requestFinished:(ASIHTTPRequest *)request 我将缩略图的 url 添加到 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”.
如果我错了,如果有人能告诉我正确的配置,那就太好了,因为过去 3 天我一直在尝试不同的方法。
【问题讨论】:
标签: iphone uitableview nsthread