我有一个自定义的uitableviewcell,我在其中使用UIImageView。我希望单元格能够根据图像视图宽度根据某个纵横比调整其高度,但我不断遇到这些约束问题:
"<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方法中添加一次。在&#34; drawRect&#34;方法我更新&#34; photoHeight&#34;常量并强制布局更新。
结果: 在第一次加载时,单元格看起来很糟糕,但在我滚动后,它们会更新它们的高度并按预期显示。我认为问题出在&#34; photoHeight&#34;第一次加载单元格时,常量不会更新,但考虑到它取决于imageview的宽度,我不知道如何修复它。 你能帮忙解决这个问题吗?
我附了一些截图: 滚动之前
滚动后
正如您在第一个屏幕截图中看到的那样,图像离开了单元格内容视图,因为该图像的单元格较小...但是,滚动后单元格会正确调整大小。我想让它们从头开始调整大小,而不仅仅是在滚动之后。
答案 0 :(得分:0)
我弄清楚导致我的版本的原因。
我在单元格的生命周期中分配的值太晚了。当我将单元格出列时,我正在设置单元格模型的属性,然后将UIViews(UILabels,在我的情况下)的赋值推迟到layoutSubviews()
。这最终导致了一个问题,即在我实际设置UILabels的文本之前都已计算出尺寸。
我建议您在单元格出列时进行所有显示设置:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
{
var cell = tableView.dequeueReusableCellWithIdentifier("MyCustomCell", forIndexPath: indexPath) as MyCustomCell
cell.setUpAllTheThings(myTableDataz[indexPath.row])
return cell
}
答案 1 :(得分:-1)
您需要使用:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//calc height
return height;
}