ios - 在UITableView中利用SDWebImage,图片只出来一部分,滚动刷新后才能显示
阿神
阿神 2017-04-17 13:30:45
[iOS讨论组]

在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)
PHPz

我没用过AFN,我猜测问题可能出现在你reloadData的时候没有在主线程进行,试试把reloadData外面包个:

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

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

__weak UITableViewController *weakSelf = self;

然后在block中,使用weakSelf

高洛峰

我也遇到了这样的问题..我是自定也cell..下边这个方法没有解决..楼主还有发现更好的方法吗?
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf reloadData];
});

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号