【问题标题】:Trouble loading UITableViewCell image and textlabel simultaneously with AFNetworking使用 AFNetworking 同时加载 UITableViewCell 图像和文本标签时遇到问题
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多