ios - 在UITableView中利用SDWebImage,图片只出来一部分,滚动刷新后才能显示

 手机用户2502856053 发布于 2022-11-03 22:00

在UITableView中利用SDWebImage来缓存图片,但是第一次运行时图片只出来一部分(随机的出现1张或者2张),点击每一行或者把表格滚动出屏幕再滚动回来,图片就出现了。
不知道是什么原因?求解答,谢谢。主要代码如下:

-(void)viewDidLoad{
    [super viewDidLoad];
//加载数据
    [self setUpStatusData];
}

-(void)setUpStatusData{
    AFHTTPRequestOperationManager *mgr=[AFHTTPRequestOperationManager manager];
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    NSString *filePath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.data"];
    WPAccount *account=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    params[@"access_token"]=account.access_token;
    [mgr GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *dictArray=responseObject[@"statuses"];
self.statuses=[WPStatus objectArrayWithKeyValuesArray:dictArray];
        //刷新表格
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    WPStatus *status=self.statuses[indexPath.row];
    cell.textLabel.text=status.text;
    WPUser *user=status.user;
    cell.detailTextLabel.text=user.name;
//加载图片,用框架,有缓存
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage imageWithName:@"icon"]];
    return cell;
}
2 个回答
  • 我也遇到了这样的问题..我是自定也cell..下边这个方法没有解决..楼主还有发现更好的方法吗?
    dispatch_async(dispatch_get_main_queue(), ^{
    [weakSelf reloadData];
    });

    2022-11-12 01:42 回答
  • 我没用过AFN,我猜测问题可能出现在你reloadData的时候没有在主线程进行,试试把reloadData外面包个:

     dispatch_async(dispatch_get_main_queue(), ^{
       [weakSelf reloadData];
     });
    

    你这个代码还有个问题,就是block中访问self会造成内存循环引用,正确的做法是:

    __weak UITableViewController *weakSelf = self;
    

    然后在block中,使用weakSelf

    2022-11-12 01:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有