【问题标题】:laggy scrolling tableview with images带有图像的延迟滚动表格视图
【发布时间】:2016-03-15 20:45:52
【问题描述】:

我将图像添加到我的 tableView 单元格中,它使它变得迟钝。我对目标 c 还很陌生,我不明白是什么原因造成的或如何解决它。非常感谢任何帮助!

group[PF_GROUP_LOGO] 只是我数据库中每个对象唯一的字符串。代码有效,只是在尝试滚动时非常滞后。

//-------------------------------------------------------------------------------------------------------------------------------------------------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    PFObject *group = groups[indexPath.row];


    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:group[PF_GROUP_LOGO]]]];
    cell.imageView.image = image;

    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d users", (int) [group[PF_GROUP_MEMBERS] count]];
    cell.detailTextLabel.textColor = [UIColor lightGrayColor];


    return cell;
}

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    有很多工具可以帮助您解决这个问题。

    从根本上说,问题在于您在主线程上运行了一个长进程,这会阻塞 UI。从非本地 URL 加载图像非常耗时,您应该在后台线程中执行此操作,这样 UI 就不会被阻塞。

    同样,有很多方法可以做到这一点,我强烈建议您对异步资源加载进行一些研究,但这是您可以在自己的示例范围内做的一件事:

    //-------------------------------------------------------------------------------------------------------------------------------------------------
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    //-------------------------------------------------------------------------------------------------------------------------------------------------
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    
        PFObject *group = groups[indexPath.row];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ // go to a background thread to load the image and not interfere with the UI
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:group[PF_GROUP_LOGO]]]];
            dispatch_async(dispatch_get_main_queue(), ^{ // synchronize back to the main thread to update the UI with your loaded image
                cell.imageView.image = image;
            });
        });
    
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%d users", (int) [group[PF_GROUP_MEMBERS] count]];
        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
    
        return cell;
    }
    

    我还建议使用AFNetworking,因为authorUIImageView 之上构建了一个非常好的类别,允许您在后台自动从Web URL 加载图像。同样,关于这个过程有很多思想流派,这只是一个想法。我建议阅读 this 以获取有关该主题的完整教程。

    希望对你有帮助!

    【讨论】:

    • 谢谢!这几乎是完美的!但是,我还有一个问题,为什么当我滚动到底部然后快速滚动回顶部时,某些图像似乎发生了变化?我该如何解决?
    • 这是一个缓存问题,完全是另一个蠕虫包。同样,AFNetworking 是你的朋友 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 2013-11-09
    • 1970-01-01
    相关资源
    最近更新 更多