Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以编程方式在UITableView单元格中显示UIStackView_Uitableview_Label_Uistackview - Fatal编程技术网

以编程方式在UITableView单元格中显示UIStackView

以编程方式在UITableView单元格中显示UIStackView,uitableview,label,uistackview,Uitableview,Label,Uistackview,我想显示包含3个标签的水平视图,但我看不到标签 override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) let stackView = UIStackView(arrangedSubviews: [label1,label2,label3]) stackView.dist

我想显示包含3个标签的水平视图,但我看不到标签

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    let stackView = UIStackView(arrangedSubviews: [label1,label2,label3])
    stackView.distribution = .fill
    stackView.axis = .horizontal
    stackView.spacing = 8
    stackView.alignment = .center
    backgroundColor = .black
    addSubview(stackView)


    stackView.leftAnchor.constraint(equalTo: leftAnchor, constant: 8).isActive = true
    stackView.rightAnchor.constraint(equalTo: rightAnchor, constant: 8).isActive = true
    stackView.topAnchor.constraint(equalTo: topAnchor, constant: 8).isActive = true
    stackView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 8).isActive = true
}
我尝试过锚,但也没有成功。 怎么做?

几个注意事项

  • 您应该将子视图添加到单元格的
    contentView
    ,而不是单元格本身

  • 从您显示的代码中,您没有设置
    stackView.translatesAutoResizezingMaskintoConstraints=false

  • 您应该使用
    leadingAnchor
    trailingAnchor
    而不是左/右

  • 这是一个启动单元,可以帮助您顺利完成任务:

    class BogdanCell: UITableViewCell {
    
        let label1: UILabel = {
            let v = UILabel()
            v.translatesAutoresizingMaskIntoConstraints = false
            v.backgroundColor = .yellow
            v.text = "Label 1"
            v.textAlignment = .center
            return v
        }()
    
        let label2: UILabel = {
            let v = UILabel()
            v.translatesAutoresizingMaskIntoConstraints = false
            v.backgroundColor = .yellow
            v.text = "Label 2"
            v.textAlignment = .center
            return v
        }()
    
        let label3: UILabel = {
            let v = UILabel()
            v.translatesAutoresizingMaskIntoConstraints = false
            v.backgroundColor = .yellow
            v.text = "Label 3"
            v.textAlignment = .center
            return v
        }()
    
        let theStackView: UIStackView = {
            let v = UIStackView()
            v.translatesAutoresizingMaskIntoConstraints = false
            v.axis = .horizontal
            v.alignment = .center
            v.distribution = .fill
            v.spacing = 8
            return v
        }()
    
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            commonInit()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            commonInit()
        }
    
        func commonInit() -> Void {
    
            backgroundColor = .black
    
            theStackView.addArrangedSubview(label1)
            theStackView.addArrangedSubview(label2)
            theStackView.addArrangedSubview(label3)
    
            contentView.addSubview(theStackView)
    
            NSLayoutConstraint.activate([
                theStackView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor, constant: 0.0),
                theStackView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor, constant: 0.0),
                theStackView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor, constant: 0.0),
                theStackView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor, constant: 0.0),
                ])
    
        }
    
    }