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

时间:2014-03-20 06:22:12

标签: ios uitableview reloaddata uirefreshcontrol

我在ViewController中有一个包含自定义单元格的UITableView。 首次加载视图时,以下代码中的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];而且刷新了工作,所以我怀疑在完成的块中获取indexPath时出了什么问题,有人可以帮帮忙吗?我需要完成的块,因为我正在进行一些图像处理。

2 个答案:

答案 0 :(得分:3)

嗨,我终于修好了。

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

即可

dispatch_async(dispatch_get_main_queue(), ^{ });

检索indexPathForCell的部分。

答案 1 :(得分:0)

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


    if (cell == nil)
        {
                nib = [[NSBundle mainBundle] loadNibNamed:@"PostCell" owner:self options:nil];
}
    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);
                       }
                   }
    ];

使用它可以帮助你