【发布时间】:2015-03-24 06:09:30
【问题描述】:
我遇到了一个与 SE 上的其他问题类似的问题,因为我的 UITableView 控制器会立即加载文本标签,但仅在我滚动视图并将项目移出屏幕时才加载我的缩略图。
我尝试将 [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;
}
【问题讨论】:
标签: ios objective-c uitableview