如何动态约束单元格contentView的内部subViews? 这是结果

问题描述

在collectionCell的conentView中,我想根据不同的值限制具有不同高度的垂直子视图,但是问题是,当内容从屏幕上滚动出然后在屏幕上向后滚动时,高度会意外地改变:>

初始和正确的视图高度:

enter image description here

向下滚动屏幕,然后向后滚动(由于未知原因,视图的高度已更改):

enter image description here

下面是布局代码:

    let contentView = cell.contentView
    let row = indexPath.row
    
    //calculate view height: relativeHeight value
    let cellHeight = cell.frame.height
    var relativeHeight = 0.6 * cellHeight
    
    let values = [112.0,116.0,86.0,95.0,67.0,76.0,34.0,43.0,24.0,35.0,47.0,66.0,57.0,36.0,64.0,23.0,22.0,20.0,23.0]
    
    let Δvalue = values.max()! - values.min()!
    
    let value = values[row]
    if Δvalue != 0 {
        let different = value - values.min()!
        let ratio = CGFloat(different / Δvalue)
        relativeHeight = ratio * relativeHeight
    }
    
    if contentView.subviews.count == 0 {
        //time label
        let timeLabel = UILabel()
        timeLabel.tag = 3
        timeLabel.textColor = self.textColor
        timeLabel.font = UIFont.boldSystemFont(ofSize: 13)
        contentView.addSubview(timeLabel)
        
        timeLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            timeLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor,constant: -4),timeLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor)
        ])
        
        //vertical view 
        let valueView = UIView()
        valueView.tag = 1
        valueView.backgroundColor = UIColor.systemBlue.withAlphaComponent(0.382) //1-0.618
        
        contentView.addSubview(valueView)
        
        valueView.translatesAutoresizingMaskIntoConstraints = false
        //valueView.constraints.forEach{ $0.isActive = false }
        
        NSLayoutConstraint.activate([
            valueView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),valueView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),valueView.bottomAnchor.constraint(equalTo: timeLabel.topAnchor,valueView.heightAnchor.constraint(equalToConstant: relativeHeight)
        ])
        
        //value label
        let valueLabel = UILabel()
        valueLabel.tag = 2
        valueLabel.textColor = self.textColor
        valueLabel.font = UIFont.systemFont(ofSize: 12)
        contentView.addSubview(valueLabel)
        
        valueLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            valueLabel.bottomAnchor.constraint(equalTo: valueView.topAnchor,valueLabel.centerXAnchor.constraint(equalTo: valueView.centerXAnchor)
        ])
        
    }

如何解决此类布局约束问题?

非常感谢!

解决方法

我猜这是因为分配后没有参考valueView或您要设置的heightConstraint,请检查contentView.subviews.count == 0。您为UICollectionViewCell准备视图层次结构的方式可能根本不是一个好习惯。在使可重用视图出队时,必须满足所有条件才能设置动态值。如果contentView.subviews.count == 0失败,则这里没有任何处理程序。

使用子类UICollectionViewCell来修复CollectionViewCell: UICollectionViewCell中所有与视图相关的任务,从而完成了修复。我添加了一个属性valueViewHeightConst,其初始约束值为0.0。我要在计算要设置的高度值后设置确切的值。

还引入了一种缓存机制,您无需在每次使单元出队时都计算relativeHeight

请检查位于https://github.com/sauvikapple/StackoverflowAnsToQ63743769的最终项目。

这是结果

enter image description here

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...