UICollectionView仅显示最后一个单元格

问题描述 投票:0回答:1

我正在以编程方式对菜单进行collectionView。如此基本,我按下一个按钮,菜单从屏幕底部出现。这正在起作用,问题是,我正在尝试在collectionview上设置标签。我设置了所有内容,但是当我运行该应用程序时,仅显示最后一个单元格。这是我的CollectionView:

class SettingsLauncher: NSObject, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {


    private var blackView = UIView()

    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = .white
        return cv
    }()
   override init() {
        super.init()
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(MenuCell.self, forCellWithReuseIdentifier: K.CollectioViewCell.cell_identifier_menu)
    }

    //MARK: - Collection View DataSource

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        3
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: K.CollectioViewCell.cell_identifier_menu, for: indexPath)
        return cell
    }    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: 60)
    }
}

这是我的单元格配置:

class BaseCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    func setupView() {

    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


let nameLabel: UILabel = {
    let label = UILabel()
    label.text = "Test"
    label.backgroundColor = .red
    return label
}()

let labelArray = ["nameLabel" : nameLabel]

class MenuCell: BaseCell {


    override func setupView() {
        super.setupView()
        backgroundColor = .blue
        addSubview(nameLabel)
        let horizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|-[nameLabel]-|", options: [], metrics: nil, views: labelArray)
        let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[nameLabel]|", options: [], metrics: nil, views: labelArray)

        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate(horizontalConstraint)
        NSLayoutConstraint.activate(verticalConstraint)


    }
}

Here is the result:

ios uicollectionview uicollectionviewcell swift5
1个回答
0
投票

视图只能有一个超级视图。您的nameLabel创建一次,然后添加到每个MenuCell中,这意味着您的标签已从以前的单元中删除。为每个单元格创建一个UILabel,您的代码应该可以正常工作,如下所示:

class MenuCell: BaseCell {

   let nameLabel: UILabel = {
       let label = UILabel()
       label.text = "Test"
       label.backgroundColor = .red
       return label
   }()

    override func setupView() {
        super.setupView()
        backgroundColor = .blue
        addSubview(nameLabel)
        let horizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|-[nameLabel]-|", options: [], metrics: nil, views: labelArray)
        let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[nameLabel]|", options: [], metrics: nil, views: labelArray)

        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate(horizontalConstraint)
        NSLayoutConstraint.activate(verticalConstraint)


    }
}
© www.soinside.com 2019 - 2024. All rights reserved.