[英]UITableView content of a row loads after scrolling the cell out of the view
问题是,在我将单元格滚动出视图之后,该单元格的内容会加载:我不知道如何正确描述它,所以我录制了一个短短的10秒视频:http: //youtu.be/GJouA3IXY0M
而且我不知道如何解决...
这是我的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;
}
您最有可能用后台线程重新加载表。 使用主线程进行UI更新。 因此,在后台获取数据后,请使用此代码。
dispatch_async(dispatch_get_main_queue(), ^{
[self.myTableView reloadData];
});
编辑:
由于您已经使用主线程重新加载了表,因此我怀疑映像需要花费一些时间来下载,因而又阻塞了线程。 我还观察到您的视频中发生了一些小故障。 尝试使用异步方法下载图像并进行检查。 initWithContentsOfURL
将阻止您的线程,在这个地方不是一个好的选择。
试试这个代码。 如有任何疑问,请通知我...
- (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];
});
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.