0
votes

I have a UITableView in a ViewController which contains a custom cell. When the view first loads, the indexPathForCell in the following code returns the indexPath without problems:

- (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);
                           }
                       }
        ];
}

After refreshing the tableView, currentIndexPath is always (null). The following is my refresh code:

- (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];
    }];

If I just perform [tableView reloadData] without performing refreshMainView, there will not be any problem with getting the currentIndexPath.

There must be something I am overlooking, could someone please help me? Thank you!

Edit: I have tried [postCell.postImageView setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:nil]; instead and the refreshing works, so I am suspecting that something is wrong with me getting the indexPath in the completed block, can anyone help out please? I need the completed block as I am doing some image processing.

2
Calling beginUpdates and endUpdates in your completion block is unnecessary as you don't insert or remove cells. - Koraktor
might be it happens because of setCompletionBlockWithSuccess Block that refresh your table view before fill post Array. - Nitin Gohel
Hi Nitin, could you elaborate? I have tried using [self performSelectorOnMainThread:@selector(reloadTableView) withObject:nil waitUntilDone:NO]; but it does not help. According to link the reload is also within the completionBlock. - Leon Liang
Where do imageURL, postData, newPostArray variables come from ? Please show more code. - HoanNguyen
@HoanNguyen hi, I have updated with more code. - Leon Liang

2 Answers

3
votes

Hi I have finally fixed it.

For people who are needing answers, just add

dispatch_async(dispatch_get_main_queue(), ^{ });

around the portion that is retrieving the indexPathForCell.

0
votes
- (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);
                       }
                   }
    ];

use this it may help you