在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; }
我也遇到了这样的问题..我是自定也cell..下边这个方法没有解决..楼主还有发现更好的方法吗?
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf reloadData];
});
我没用过AFN,我猜测问题可能出现在你reloadData
的时候没有在主线程进行,试试把reloadData
外面包个:
dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf reloadData]; });
你这个代码还有个问题,就是block
中访问self
会造成内存循环引用,正确的做法是:
__weak UITableViewController *weakSelf = self;
然后在block
中,使用weakSelf