iOS 延迟加载表格图像 [英] iOS lazy-loading of table images
问题描述
我已经使用苹果提供的示例在我的应用中成功实现了图像的延迟加载 这里.问题是,我希望在滚动时加载图像,但仅当我完成拖动时图像才会加载到单元格中(从屏幕上松开手指.在此之前单元格保持为空).我将代码修改如下:
I have successfully implemented lazy-loading of images in my app using the example provided by apple here. The thing is that, I want the image to load as I am scrolling, but the image loads into the cell only when I finish dragging (Release my finger from the screen. Until then the cell remains empty). I have modified the code as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"cell for row at indexpath, size - %f, current - %d", flowTable.contentSize.height, self.current);
NSString *CellIdentifier = @"FlowCell";
MyFlowCell *cell = (MyFlowCell *)[self.flowTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FlowCell" owner:nil options:nil];
// cell = [nib objectAtIndex:0];
for(id currentObject in nib)
{
if([currentObject isKindOfClass:[MyFlowCell class]])
{
cell = (MyFlowCell *)currentObject;
break;
}
}
}
ImageDetails *rowItem = [self.imageData objectAtIndex:indexPath.row];
if (!rowItem.mainImage)
{
// if (self.flowTable.dragging == NO && self.flowTable.decelerating == NO)
// {
[self startIconDownload:rowItem forIndexPath:indexPath];
//}
cell.mainImage.backgroundColor = [UIColor grayColor];
}
else
{
cell.mainImage.image = rowItem.mainImage;
}
}
return cell;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate)
{
// [self loadImagesForOnscreenRows];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//[self loadImagesForOnscreenRows];
}
此外,我发现在我的 ImageDownloader 类中,NSURLConnection 甚至在我从屏幕上松开手指之前都没有收到响应.我一直试图找出为什么会发生这种情况,但到目前为止我还没有成功.如果我遗漏了什么,请告诉我.
Moreover, I found out that in my ImageDownloader class, the NSURLConnection doesn't even receive the response until I release my finger from the screen. I have been trying to find out why this is happening, but I haven't been successful so far. Please tell me if I've been missing something.
推荐答案
您可以按照以下步骤在tableview中实现延迟加载:
You can achieve lazy loading in your tableview by following these steps :
在您的 Class.h 中,输入:
In your Class.h, put:
NSMutableDictionary *Dict_name;
BOOL isDragging_msg, isDecliring_msg;
现在在 Class.m 文件中,将这段代码放在视图中确实加载了:
Now In Class.m file, put this code in view did load:
Dict_name = [[NSMutableDictionary alloc] init];
在 cellForRowAtIndexPath 中:
In cellForRowAtIndexPath:
if ([dicImages_msg valueForKey:[[msg_array objectAtIndex:indexPath.row] valueForKey:@"image name or image link"]]) {
cell.image_profile.image=[dicImages_msg valueForKey:[[msg_array objectAtIndex:indexPath.row] valueForKey:@"image name or image link"]];
}
else
{
if (!isDragging_msg && !isDecliring_msg)
{
[dicImages_msg setObject:[UIImage imageNamed:@"Placeholder.png"] forKey:[[msg_array objectAtIndex:indexPath.row] valueForKey:@"image name or image link"]];
[self performSelectorInBackground:@selector(downloadImage_3:) withObject:indexPath];
}
else
{
cell.image_profile.image=[UIImage imageNamed:@"Placeholder.png"];
}
}
对于下载图片,函数是:
And for the download image the function is:
-(void)downloadImage_3:(NSIndexPath *)path{
NSAutoreleasePool *pl = [[NSAutoreleasePool alloc] init];
NSString *str=[here Your image link for download];
UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]];
[dicImages_msg setObject:img forKey:[[msg_array objectAtIndex:path.row] valueForKey:@"image name or image link same as cell for row"]];
[tableview performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
[pl release];
}
最后把这些方法放到你的类中:
And at last put these methods in your class:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
isDragging_msg = FALSE;
[tableview reloadData];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
isDecliring_msg = FALSE;
[tableview reloadData];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
isDragging_msg = TRUE;
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
isDecliring_msg = TRUE;
}
这篇关于iOS 延迟加载表格图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!