0

I'm using UITableViewController for displaying data from Parse. It runs perfectly on my Xcode Simulator as i think there's no latency in network. But whenever i'm uploading the code to AppStore for Testing. The very first time i run the app it has to load a couple of restaurant's from Parse and display in UITableViewController. Upon clicking a row the first rows data is being loaded into the 3rd row and 4th row data loading in 6th row data irregularly. Why is the data being loaded very unevenly ? Here's my

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifier  = @"restaurantIdentifier";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    PFObject *tempObject = [self.objectArray objectAtIndex:indexPath.row];

    PFFile *imageFile = [tempObject objectForKey:@"RestaurantIcon"];
    PFImageView *imageView = [[PFImageView alloc] init];
    imageView.file = imageFile;

    [imageView loadInBackground:^(UIImage *img,NSError *error){

        if(!error){
            cell.imageCell.image = imageView.image;
        }
    }];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
        cell.imageView.layer.masksToBounds = YES;
        cell.imageView.layer.cornerRadius = 4;

        cell.imageView.frame = self.view.bounds;
        cell.cellLabel.text = [tempObject objectForKey:@"RestaurantName"];
        [self.hotelNamesArray addObject:[tempObject objectForKey:@"RestaurantName"]];

        cell.cellLabel.lineBreakMode = NSLineBreakByWordWrapping;

    return cell;
}




- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    _restaurantName = [self.hotelNamesArray objectAtIndex:indexPath.row];

        self.restaurantMenuNameArray = [[NSMutableArray alloc] init];

        PFQuery *query = [PFQuery queryWithClassName:[self.hotelNamesArray objectAtIndex:indexPath.row]];

        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

                    if (!error) {
            for (PFObject *obj in objects) {

                if (![_restaurantMenuNameArray containsObject:[obj objectForKey:@"RestaurantMenuName"]]) {

                    NSLog(@"restaurantmenunames are %@",[obj objectForKey:@"RestaurantMenuName"]);

                    if ([obj objectForKey:@"RestaurantMenuName"] ==nil) {
                        [self performSegueWithIdentifier:@"restaurantDetail" sender:self];
                        return;
                    }else {
                        [_restaurantMenuNameArray addObject: [obj objectForKey:@"RestaurantMenuName"]];
                    }
                }
            }
        }else {
                // Log details of the failure
                NSLog(@"Error: %@ %@", error, [error userInfo]);
            }
            [self.tableView reloadData];
            NSLog(@"restaurantMenuNames is %@",_restaurantMenuNameArray);
            [self performSegueWithIdentifier:@"restaurantDetail" sender:self];
        }];
}

Thanks in advance.

1 Answer 1

1

If you mean the images get in the wrong cell, you have to consider that cells are recycled when you scroll, and that if the image loading takes a bit too long, you may get the result after the cell has been reused.

You need to check that the cell is still for the item/row you want (you could store the row in the cell's tag and check it before setting the image in the completion handler, for instance).

If it's other data that is mixed up, then you'll need to show us the code that loads that data.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your prompt reply jcaron. If i just do cell.cellLabel.tag = indexPath.row; in my cellForRowAtIndexPath and something like this [imageView loadInBackground:^(UIImage *img,NSError *error){ if(!error){ cell.imageCell.image = imageView.image; cell.imageCell.tag = indexPath.row; } }]; will that suffice ?
No, you need to check the tag before changing the image, not assign it.
I assigned it in cellForRowAtIndexPath [imageView loadInBackground:^(UIImage *img,NSError *error){ if(!error){ _cell.imageCell.image = imageView.image; _cell.imageCell.tag = indexPath.row; } }]; and checked it inside didSelectRowAtIndexPath.... if (!error && (_cell.imageCell.tag = indexPath.row)) {.... it's working now... but i'm unable to reproduce the issue as it only occurs whenever there's a delay in network

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.