ios - 滚动 UITableView 时图像内容出现偏移

标签 ios objective-c uitableview uiscrollview

滚动自定义 UITableView 单元格的 TableView 时出现一些奇怪的行为。

应用首次打开时,内容正常:

Initial load of the app

但是如果我滚动我的 View ,一些偏移量会出现在嵌入式 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 只是 call 的一个简单子(monad)类,其元素指向 Storyboard 中的 IBOutlets:

Storyboard section

最佳答案

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

下面:

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

关于ios - 滚动 UITableView 时图像内容出现偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23230557/

相关文章:

iphone - Cocos2d 按钮被点击后,定时器增加 3 秒

ios - 在节点中使用 sprite kit 问题进行游戏开发?

ios - 键盘存在时如何使 UITextField 向上移动?

ios - 如何在 SceneKit iOS 中更改 3D 对象的高度宽度

ios - 为什么有时变量在 block 中变为 NULL

ios - 不使用自动布局时,PrepareForSege崩溃

ios - 从不同的 View Controller 访问实例变量

performance - UITableView 加载更多内容而无需重新加载完整表格

ios - UITableViewController 和 UICollectionViewController 的 .view 属性

uitableview - rectForRowAtIndexPath 不适用于 iOS8