我有表格视图名称“tableAll”,数组“thumbVideoListArray”(首先这个数组是空的)并且有数组“objDelegate.listVidArray”,它有图像的url。我认为我有错误检查。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([thumbVideoListArray count] == 0)
{
[NSThread detachNewThreadSelector:@selector(fetchImage:) toTarget:self withObject:indexPath];
}
else
{
UIImage *img1 = [thumbVideoListArray objectAtIndex:indexPath.row];
if(img1)
{
cell.thumbImage = [thumbVideoListArray objectAtIndex:indexPath.row];
}
else
{
[NSThread detachNewThreadSelector:@selector(fetchImage:) toTarget:self withObject:indexPath];
}
}
}
- (void)fetchImage:(NSIndexPath *)indexPath
{
NSLog(@"in thead");
if([[[[objDelegate.listVidArray objectAtIndex:[objDelegate.listVidArray count]-(indexPath.section+1)]valueForKey:@"videoList"]valueForKey:@"media_id"]objectAtIndex:indexPath.row])
{
NSData *dataImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[[[objDelegate.listVidArray objectAtIndex:[objDelegate.listVidArray count]-(indexPath.section+1)]valueForKey:@"videoList"]valueForKey:@"img"]objectAtIndex:indexPath.row]]]];
if([dataImage length])
{
UIImage *imageThumb = [UIImage imageWithData:dataImage];
[thumbVideoListArray addObject:imageThumb];
// Reload rows with the fetched image in other thread
[self performSelectorOnMainThread:@selector(reloadTable:) withObject:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] waitUntilDone:NO];
}
}
}
- (void)reloadTable:(NSArray *)array
{
[tableAll reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];
}