NSThread, UITableViewCell and Image-Collection of common programming errors
i am currently having some problem with my UITableView, this is the first time I am putting thumbnail in my table view cell and also integrating NSThread to load thumbnail image, so, please bare with me. Thanks
I am downloading the data and setting up the cells like usual, but this time with the NSThread added.
First I will call ASIHTTPRequest in my view did load
Then in – (void)requestFinished:(ASIHTTPRequest *)request I will add the url of the thumbnail into an NSMutableArray
NSString *photoURLString = [NSString stringWithFormat:@"http://some.url.com/img/%@",[thearray objectForKey:@"tn_url"]];
[thumbNailURL addObject:(photoURLString)];
And in the cellForRowAtIndexPath: where I am setting up other data to display, I will call my NSThread
[NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:indexPath];
And in my loadImage thread
- (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];
}
All the data and image loaded just fine, but if I scroll faster up and down , the application will hang with the following error
[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".
It would be great if anyone could show me the correct configuration if I am wrong, as I have been trying different approches for the past 3 days already. Thank you so much.