UICollectionView 只显示最后一个单元格

Posted

技术标签:

【中文标题】UICollectionView 只显示最后一个单元格【英文标题】:UICollectionView only show last cell 【发布时间】:2020-06-13 19:10:37 【问题描述】:

我正在以编程方式为菜单执行 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)


    

【问题讨论】:

【参考方案1】:

视图只能有一个超级视图。您的 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)


    

【讨论】:

谢谢!它现在正在工作。但是我需要像这样删除我的数组并使用 Anchor 构建: override func setupView() super.setupView() backgroundColor = .blue addSubview(nameLabel) nameLabel.translatesAutoresizingMaskIntoConstraints = false nameLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor) .isActive = true nameLabel.widthAnchor.constraint(equalToConstant: contentView.frame.width).isActive = true nameLabel.heightAnchor.constraint(equalToConstant: contentView.frame.height).isActive = true

以上是关于UICollectionView 只显示最后一个单元格的主要内容,如果未能解决你的问题,请参考以下文章

UICollectionView 仅显示最后一部分的页脚

无论在 UICollectionView 中选择啥 IndexPath,只有最后一个单元格会被突出显示

UICollectionView 只显示 2 组数据

UICollectionView 显示两行,但我只想要一行

如何使用 UITableview 或 UICollectionView 在表格的最后一行显示数字列的总值?

动画 UICollectionView 的高度变化