-1
“从后台线程修改自动布局引擎”为了我的UITableViewCell流利下载和显示图像,我已经实现了以下内容:在一个UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *thisCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (_movieCell == nil) {
thisCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Movie* thisMovie = [_movies objectAtIndex:indexPath.row];
thisCell.imageView.image = nil;
thisCell.textLabel.text =thisMovie.Title;
thisCell.imageView.image=[UIImage imageNamed:@"Dummy"];
thisCell.detailTextLabel.text = thisMovie.Year;
NSURL * imageURL = [NSURL URLWithString:[thisMovie.Poster stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:imageURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
UIImage *image = [UIImage imageWithData:data];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
if (updateCell)
updateCell.imageView.image = image;
//[updateCell setNeedsLayout];
});
}
}
}];
[task resume];
return thisCell;
}
这一切都工作得非常好,除了事实上,该代码的可见部分的至少执行了几秒钟后,调试控制台告诉我
此应用程序从后台 线程修改自动版式发动机发动机是从访问后主线程。
这是一个非常简单的应用程序的一部分,没有任何其他背景的事情发生,所以我认为这是造成这个问题的这部分代码。
任何人都可以显示我的代码中的错误,或者,或者,解释一种方法来调试/跟踪后台线程?
谢谢!
此代码片段不足以让我们找出问题出在哪里。哟显然是更新您的程序中的其他地方的背景队列中的用户界面。 – ozgur
你是对的。请参阅下面的答案。 – Sjakelien