滚动UITableView时,图像内容中会出现偏移

时间:2014-04-22 21:28:32

标签: ios objective-c uitableview uiscrollview

滚动自定义UITableView单元格的TableView时,我会遇到一些奇怪的行为。

当应用首次打开时,内容很好:

Initial load of the app

但如果我滚动我的视图,嵌入的UIImageViews中会出现一些偏移:

State after scrolling

我已删除所有布局限制,但问题仍然存在。

我因为这种情况发生原因而感到茫然;欢迎任何帮助!

这是我的cellForRowAtIndexPath代码:

- (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只是一个简单的调用子类,元素指向故事板中的IBOutlets:

Storyboard section

1 个答案:

答案 0 :(得分:1)

这很奇怪,试试看它是否有效

下面:

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

尝试添加:

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

这会将图像的原点设置为(0,0)。

编辑:您也可以尝试添加以下行。

[cell.restaurantImage sizeToFit];