ios - UITableViewCell 子类,滚动前高度错误

标签 ios uitableview height autolayout

我有一个自定义的 uitableviewcell,我在其中使用了 UIImageView。我希望单元格能够根据特定的纵横比调整其高度,基于 imageview 宽度,但我不断遇到这些约束问题:

"<NSLayoutConstraint:0x7ffe13c14130 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ffe117fb920(44)]>",
"<NSLayoutConstraint:0x7ffe13c15090 V:|-(0)-[UIView:0x7ffe13c11c10]   (Names: '|':UITableViewCellContentView:0x7ffe117fb920 )>",
"<NSLayoutConstraint:0x7ffe13c150e0 V:[UIView:0x7ffe13c11c10]-(0)-[UIButton:0x7ffe117e8110]>",
"<NSLayoutConstraint:0x7ffe13c15190 V:[UIButton:0x7ffe117e8110(30)]>",
"<NSLayoutConstraint:0x7ffe13c151e0 V:[UIButton:0x7ffe117e8110]-(0)-[UIView:0x7ffe13c11d00]>",
"<NSLayoutConstraint:0x7ffe13c15910 UIView:0x7ffe13c11d00.height == UIView:0x7ffe13c11c10.height>",
"<NSLayoutConstraint:0x7ffe13c15960 V:[UIView:0x7ffe13c11d00]-(0)-[UIButton:0x7ffe13c0b000]>",
"<NSLayoutConstraint:0x7ffe13c159b0 UIButton:0x7ffe13c0b000.height == UIButton:0x7ffe117e8110.height>",
"<NSLayoutConstraint:0x7ffe13c15a50 V:[UIButton:0x7ffe13c0b000]-(0)-[UIView:0x7ffe13c11df0]>",
"<NSLayoutConstraint:0x7ffe13c15aa0 UIView:0x7ffe13c11df0.height == UIView:0x7ffe13c11c10.height>",
"<NSLayoutConstraint:0x7ffe13c15af0 V:[UIView:0x7ffe13c11df0]-(0)-|   (Names: '|':UITableViewCellContentView:0x7ffe117fb920 )>"

我的约束是

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[s1(>=10)][pictureView][s2(==s1)]|" options:0 metrics:nil views:dict]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[s3][markAsFavorite(==30)][s4(==s3)][markForDeletion(==markAsFavorite)][s5(==s3)]|" options:0 metrics:nil views:dict]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[s1][s3]|" options:0 metrics:nil views:dict]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[pictureView][s3]|" options:0 metrics:nil views:dict]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[s2][s3]|" options:0 metrics:nil views:dict]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[pictureView]-10-[markAsFavorite(==30)]-10-|" options:0 metrics:nil views:dict]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[pictureView]-10-[markForDeletion(==30)]-10-|" options:0 metrics:nil views:dict]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[pictureView][s5]-10-|" options:0 metrics:nil views:dict]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[pictureView][s4]-10-|" options:0 metrics:nil views:dict]];


    photoHeight = [NSLayoutConstraint constraintWithItem:pictureView
                                               attribute:NSLayoutAttributeHeight
                                               relatedBy:0
                                                  toItem:nil
                                               attribute:NSLayoutAttributeNotAnAttribute
                                              multiplier:1
                                                constant:(5*pictureView.frame.size.width)/16];


    [self.contentView addConstraint:photoHeight];

这些约束只添加一次,在单元格子类的 updateConstraints 方法中。在“drawRect”方法中,我更新“photoHeight”常量并强制更新布局。

结果: 第一次加载时,单元格看起来很糟糕,但在我滚动后,它们会更新高度并按预期显示。我认为问题在于首次加载单元格时未更新“photoHeight”常量,但考虑到它取决于 imageview 的宽度,我不知道如何修复它。 你能帮忙解决这个问题吗?

我附上了一些截图: 滚动前 before scrolling

滚动后 after scrolling

正如您在第一个屏幕截图中看到的那样,图像超出了单元格内容 View ,因为单元格对于该图像来说太小了……但是,在滚动后单元格会正确调整大小。我希望它们从一开始就调整大小,而不仅仅是在滚动之后。

最佳答案

我弄清楚是什么导致了我的版本。

我在单元格的生命周期中赋值的时间太晚了。当我将单元格出队时,我为单元格的模型设置了一个属性,然后将 UIViews(在我的例子中是 UILabels)的分配推迟到 layoutSubviews()。这最终导致了一个问题,即在我实际设置 UILabel 的文本之前计算了所有尺寸。

我建议在您将单元格出队时进行所有显示设置:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
{
    var cell = tableView.dequeueReusableCellWithIdentifier("MyCustomCell", forIndexPath: indexPath) as MyCustomCell
    cell.setUpAllTheThings(myTableDataz[indexPath.row])
    return cell
}

关于ios - UITableViewCell 子类,滚动前高度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27013317/

相关文章:

ios - UITableView 在编辑时不显示左侧多选圆圈

javascript - 如何使用 CSS/Javascript 根据窗口高度使 div 在两个 div 之间自动调整其高度?

html - 如何在未设置高度的父 DIV 中适应 IMAGE?

ios - 单击时如何检查 ImageView 标签

c# - Unity Camera Size 网络播放器

iphone - 无法更改 UITableViewCell 背景颜色

javascript - 在不将高度设置为自动的情况下获取元素的自动高度

ios - 由于未捕获的异常 'com.firebase.core' 而终止应用程序,原因 : '[FIRApp configure] Firebase Analytics is not available.'

ios - Flutter iOS 上每种 flavor 构建的不同 google maps api key

iphone - iPhone 上 Mail.app 中的圆圈 - iOS 编程