Question

I am getting some strange behaviour when scrolling a TableView of custom UITableView cells.

When the app first opens, the content is fine:

Initial load of the app

But if I scroll my view some offsets appear in the embedded UIImageViews:

State after scrolling

I have removed all layout constraints, and the problem still occurs.

I am at loss for reasons why this happens; any help is welcomed!

Here's my cellForRowAtIndexPath code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *StrechCellIdentifier = @"StrechCell";

    //Place regular strechable cells.
    GFStrechCell *cell = [tableView dequeueReusableCellWithIdentifier:StrechCellIdentifier];

    if (cell == nil) {
        cell = (GFStrechCell*) [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:StrechCellIdentifier];
    }

    cell.nameLabel.text = @"Chez Julien";

    cell.descriptionLabel.text = @"A yummy place!";

    cell.descriptionLabel.hidden = YES;

    [cell.restaurantImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"restaurant%i.png", (indexPath.row%5 + 1)]]];

    if(indexPath.row % 2 == 0){
        [cell.favoriteButton.imageView setImage:[UIImage imageNamed:@"favoriteButtonPressed.png"]];
    } else {
        [cell.favoriteButton.imageView setImage:[UIImage imageNamed:@"favoriteButton.png"]];
    }

    return cell;
}

GFStrechCell is just a simple subclass of call with elements pointing to IBOutlets in storyboard:

Storyboard section

Was it helpful?

Solution

That's odd, try this out just to see if it works

Below:

[cell.restaurantImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"restaurant%i.png", (indexPath.row%5 + 1)]]];

Try adding:

CGRect restaurantImageFrame = cell.restaurantImage.frame;
restaurantImageFrame.origin = CGPointZero; //or whatever it should be
cell.restaurantImage.frame = restaurantImageFrame;

This will set the image's origin to (0,0).

Edit: you could also try adding the following line instead.

[cell.restaurantImage sizeToFit];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top