我使用控制台检查它是否重新加载,它在 cellForRowAtIndexPath
中重新加载。它会导致 TableView 卡住片刻。我以为这是因为图像,但现在我无法找出问题所在。
任何帮助都会很棒。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog( @"Cell loading" );
static NSString *CellIdentifier = @"showVideo";
CustomVideoCell *cell = (CustomVideoCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell=[[CustomVideoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
NSURL *url = [NSURL URLWithString:[data objectForKey:@"profilepicture"]];
NSData *data = [NSData dataWithContentsOfURL:url];
[cell.playimage setImageWithURL:[NSURL URLWithString:[data objectForKey:@"profilepicture"]]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
return cell;
}
最佳答案
这一行产生了问题:
NSData *data = [NSData dataWithContentsOfURL:url];
这是一个同步调用,这就是为什么您在滚动时会感到滞后。
切勿在主线程上调用此类方法,这会导致您的应用无响应。如果数据太高,则下载会花费更多时间,因此响应 UIEvents 的时间也会更长。
您需要在下载图像后对其进行缓存,否则每次重新加载表格时都会再次下载。如果图像已经下载,您需要从缓存中显示它,不要再次下载。您需要在 cellForRowAtIndexPath
上处理此问题。
您可以编写自己的代码来执行这些任务,也可以依赖以下示例。
检查以下库/源代码:
关于ios - 我正在使用缓存图像,但是当我在 uitableview 中向上滚动时它一直在重新加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13948479/