gpt4 book ai didi

ios - 使用 AFNetworking 同时加载 UITableViewCell 图像和文本标签时遇到问题

转载 作者:行者123 更新时间:2023-11-29 02:11:47 24 4
gpt4 key购买 nike

我遇到的问题与 SE 上的其他问题类似,因为我的 UITableView Controller 会立即加载文本标签,但只会在我 ScrollView 并将项目移出屏幕时加载我的缩略图。

我尝试将 [self.tableView reloadData] 添加到 AFHTTPRequestOperation setCompletionBlockWithSuccess,但有一个缺点。它显然运行得太频繁了。

出现问题的方法如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *fullPath;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];

Child *child = _children[indexPath.row];

if([child.data.thumbnail length] == 0) {
fullPath = @"reddit.png";
} else {

// Get the thumbnail
NSURL *url = [NSURL URLWithString:child.data.thumbnail];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

fullPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[url lastPathComponent]];

[operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:fullPath append:NO]];

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"bytesRead: %lu, totalBytesRead: %lld, totalBytesExpectedToRead: %lld", (unsigned long)bytesRead, totalBytesRead, totalBytesExpectedToRead);
}];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// [self.tableView reloadData];

NSLog(@"RES: %@", [[[operation response] allHeaderFields] description]);

NSError *error;

if(error) {
NSLog(@"ERR: %@", [error description]);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"ERR1: %@", [error description]);
}];

[operation start];
}

cell.textLabel.text = child.data.title;
cell.imageView.image = [UIImage imageNamed:fullPath ];
return cell;
}

最佳答案

不必在每次加载图像时都重新加载整个 TableView ,您可以直接在完成 block 内的单元格上设置该图像。

但是,如果您这样做,您需要检查该单元格是否仍然可见,并且它是否仍在您开始加载 View 时所在的同一索引路径上,否则您可能会将图像设置在一个单元格上已被重用,现在在 TableView 中处于不同的位置。

关于ios - 使用 AFNetworking 同时加载 UITableViewCell 图像和文本标签时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29218776/

24 4 0