简体   繁体   English

UICollectionView 只显示最后一个单元格

[英]UICollectionView only show last cell

I'm doing a collectionView programmatically for a menu.我正在以编程方式为菜单做一个collectionView。 So basic I press a button and the menu appears from the bottom of my screen.所以基本我按下一个按钮,菜单就会从我的屏幕底部出现。 That's is working, the problem is, that I'm trying to set label on my collectionview.这是有效的,问题是,我正在尝试在我的 collectionview 上设置 label。 I set everything, but when I run the app, just my last cell show up.我设置了所有内容,但是当我运行应用程序时,只显示我的最后一个单元格。 So here is my 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)
    }
}

And here is my cell configuration:这是我的单元格配置:

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)


    }
}

结果如下:

Views can only have one superview.视图只能有一个超级视图。 Your nameLabel is created once, then added to each MenuCell, which means your label is removed from the previous cells.您的 nameLabel 创建一次,然后添加到每个 MenuCell,这意味着您的 label 已从先前的单元格中删除。 Create a UILabel for each cell and your code should work fine, like so:为每个单元格创建一个 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)


    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM