ios - 重新加载 UITableView 数据后,indexPathForCell 返回 null

标签 ios uitableview reloaddata uirefreshcontrol

我在包含自定义单元格的 ViewController 中有一个 UITableView。 当 View 首次加载时,以下代码中的 indexPathForCell 会毫无问题地返回 indexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PostCell *postCell = (PostCell *)[tableView dequeueReusableCellWithIdentifier:@"PostCell"];

    if (postCell == nil) {
        NSLog(@"EmptyCell Found");
    }

    NSDictionary *object = [postArray objectAtIndex:indexPath.row];
    NSString *imageURL = [object objectForKey:@"imageURL"];

    NSIndexPath *originalIndexPath = indexPath;
    NSLog(@"Original IndexPath %@", originalIndexPath);

    SDWebImageManager *manager = [SDWebImageManager sharedManager];
        [manager downloadWithURL:[NSURL URLWithString:imageURL]
                         options:indexPath.row == 0 ? SDWebImageRefreshCached : 0
                        progress:^(NSInteger receivedSize, NSInteger expectedSize){}
                       completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished){
                           if (image) {

                               // This returns correctly on first load
                               NSIndexPath *currentIndexPath = [imageTableView indexPathForCell:postCell];
                               NSLog(@"Current IndexPath %@", currentIndexPath);
                           }
                       }
        ];
}

刷新tableView后,currentIndexPath始终为(null)。 以下是我的刷新代码:

- (void)refreshMainView {
    AFHTTPRequestOperation *downloadPostOperation = [[AFHTTPRequestOperation alloc] initWithRequest:serviceRequest];

    [downloadPostOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        //[tableView beginUpdates];

        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];
        NSMutableArray *newPostArray = [json objectForKey:@"posts"];

        // Replacing the old array with new array
        postArray = newPostArray;    
        [self stopRefresh];        
        //[tableView endUpdates]
        [imageTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self stopRefresh];
    }];

如果我只是执行[tableView reloadData]而不执行refreshMainView,那么获取currentIndexPath不会有任何问题。

一定有什么我忽略了,有人可以帮我吗?谢谢!

编辑: 我已经尝试了 [postCell.postImageView setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:nil]; 而不是刷新工作,所以我怀疑我在完成的 block 中获取 indexPath 有问题, 任何人都可以帮忙吗?我需要完成的 block ,因为我正在做一些图像处理。

最佳答案

嗨,我终于修好了。

对于需要答案的人,只需添加

dispatch_async(dispatch_get_main_queue(), ^{ });

围绕检索 indexPathForCell 的部分。

关于ios - 重新加载 UITableView 数据后,indexPathForCell 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22524980/

相关文章:

ios - reloadData 不适用于另一个 UITableView 静态单元格中的 UITableView

ios - 代号一 - 自定义 TextField

ios - 在 iPhone 6 Plus 上锁定 LaunchScreen.xib 方向

ios - 刷新 iOS 指令缓存

ios - iPhone平滑素描绘制算法

iphone - 如何在 iPhone 中的部分 tableviewcell 中添加行

ios - 快速编辑后保存表格 View 文本

ios - 如何知道 UITableView 何时完成 reloadData?

objective-c - 将 KVO 添加到 UITableViewCell

arrays - 值更改后 CollectionView reloaddata()