简体   繁体   中英

Offset appears in image content when scrolling UITableView

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

When the app first opens, the content is fine:

应用的初始加载

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

滚动后的状态

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:

故事板部分

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM