我不知道如何解决它......
这是我的cellForRowAtIndexPath方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PlaylistenTableViewCell *cell = [self.playlistenTableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//titleLabel = label which shows the big headline
cell.titleLabel.text = [self.JsonTitle objectAtIndex:indexPath.row];
cell.titleLabel.layer.cornerRadius = 5;
cell.titleLabel.layer.masksToBounds = YES;
NSString *urlString = [self.JsonContent objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
//playlistThumbnailImageView = the imageVIew which shows the Image
cell.playlistThumbnailImageView.image = tmpImage;
[cell.playlistThumbnailImageView setOpaque:NO];
[cell.playlistThumbnailImageView setBackgroundColor:color];
cell.playlistThumbnailImageView.layer.cornerRadius = 5;
cell.playlistThumbnailImageView.layer.masksToBounds = YES;
cell.playlistThumbnailImageView.layer.borderWidth = 4.0;
return cell;
}
答案 0 :(得分:1)
您很可能正在使用后台线程重新加载表。使用主线程进行UI更新。因此,在后台获取数据后,请使用此代码。
dispatch_async(dispatch_get_main_queue(), ^{
[self.myTableView reloadData];
});
编辑:
由于您已经使用主线程重新加载表,我怀疑图像需要时间下载,反过来它阻止了线程。我还观察到你的视频中发生了一些小故障。尝试使用异步方法下载图像并检查。 initWithContentsOfURL
会阻止你的帖子,在这个地方不是一个好选择。
答案 1 :(得分:0)
试试这段代码。如有疑问,请告诉我......
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
-------
-------
NSString *imageUrl = @"-- URL string --";
NSURL *urlImage = [NSURL URLWithString:imageUrl];
[imgVw setImage:[UIImage imageNamed:@"placeholder.png"]];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(queue, ^{
NSError* error = nil;
NSData *imageData = [NSData dataWithContentsOfURL:urlImage options:nil error:&error];
if(error){
dispatch_sync(dispatch_get_main_queue(), ^{
[imgVw setImage:[UIImage imageNamed:@"placeholder.png"]];
[self.loadingIndicator stopAnimating];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
});
}else{
UIImage *image = [UIImage imageWithData:imageData];
dispatch_sync(dispatch_get_main_queue(), ^{
[self.imgVw setImage:image];
[self.loadingIndicator stopAnimating];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
});
}
});
}