UICollectionViewCell:如何让每个单元格具有不同的命名

时间:2017-11-30 18:01:59

标签: ios uilabel uicollectionviewcell

我希望我的单元格具有不同的标题,但它始终显示相同的标题 我试图使用二维数组,但实际上并不知道如何实现它

任何帮助都将受到高度赞赏,

class DequedCEll: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
    setupViews()
    }

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

// this is the part where i stuck 

    let Mainlabel : UILabel = {
        var label = UILabel()
        label.text = "??"
        label.font = UIFont.boldSystemFont(ofSize: 45)

       return label
    }()

    func setupViews () {

        backgroundColor = .yellow
        addSubview(Mainlabel)

        Mainlabel.anchorToTop(self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor)

    }


}

1 个答案:

答案 0 :(得分:1)

我假设代码中的某个地方你将collectionViewCell排队。您可以在将单元格出列后立即设置标签。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: yourCellIdentifier, for: indexPath) as? DequedCEll else { return DequedCEll() }

    cell.Mainlabel.text = "your text here from you array, probably using indexPath to determine what text to use"
    return cell
}