테이블 뷰 이름이 "tableAll", 배열 "thumbVideoListArray"(처음에는이 배열이 비어 있습니다.) 이미지의 URL을 가진 배열 "objDelegate.listVidArray"가 있습니다. 실수 체크가 있다고 생각합니다.Tableview 지연로드 문제
- (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];
}
무엇이 문제입니까? –
fetchImage 메서드를 호출 할 때마다 –
[SDWebImage] (https://github.com/rs/SDWebImage)가 lazyloading을위한 최선의 코드입니다. 쉽고 부드럽게 :) – TheTiger