Sneezy
Sneezy

Reputation: 69

When scrolling in the table, cells change place

#import "UIImageView+AFNetworking.h"

When i scroll in my table. The cells change places. It's like he doesn't know what the first cell is annymore.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

    if(indexPath.section == 0 )
    {
        Recipe *dish = [_recipes objectAtIndex:indexPath.row];
        cell.textLabel.text = dish.user;
        cell.imageView.image = _imageView.image;
        return cell;
    }
    else if (indexPath.section == 1 || indexPath.section == 2)
    {
        Recipe *dish = [_recipesExtra objectAtIndex:indexPath.row];
        cell.textLabel.text = dish.title;
        [cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: dish.url]] 
                              placeholderImage:nil 
                              success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                           [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
                                       } 
                              failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                       }];
        return cell;
    }
    else
    {
        return nil;
    }


}

I'm sure that my mistake is in the reloadRowsAtIndexPaths, but i don't know how to fix it.

Upvotes: 0

Views: 339

Answers (1)

Sneezy
Sneezy

Reputation: 69

I made a new property where i can save the image, so when a reload occurs he simply takes the image that is "saved" so he doesn't need to load the image from the internet.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

    if(indexPath.section == 0 )
    {
        Recipe *dish = [_recipes objectAtIndex:indexPath.row];
        cell.textLabel.text = dish.user;
        cell.imageView.image = _imageView.image;
        return cell;
    }
    else if (indexPath.section == 1 || indexPath.section == 2)
    {
        Recipe *dish = [_recipesExtra objectAtIndex:indexPath.row];
        cell.textLabel.text = dish.title;
        if (dish.image) {
            cell.imageView.image = dish.image;
        } 
        else 
        {
            [cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: dish.url]] 
                                  placeholderImage:nil 
                                           success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                               dish.image = image;
                                               [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
                                           } 
                                           failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                           }];

        }

        return cell;
    }
    else
    {
        return nil;
    }
}

Upvotes: 1

Related Questions