UICollectionView 中的自定义单元格内容

Custom Cell contents in UICollectionView

我正在尝试以编程方式在 swift class 中添加 UICollectionView,没有任何故事板,带有自定义单元格(每个单元格中有一个简单的标签)

import Foundation
import UIKit

class BoardView : UIView, UICollectionViewDataSource, UICollectionViewDelegate {

    var boardTable: UICollectionView!
    var cellLabel:UILabel!
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

    var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

    convenience init(frame: CGRect, title: String) {
        self.init(frame: frame)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        layout.sectionInset = UIEdgeInsets(top: 60, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 30, height: 30)
        boardTable = UICollectionView(frame: self.frame, collectionViewLayout: layout)
        boardTable.dataSource = self
        boardTable.delegate = self
        boardTable.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        boardTable.backgroundColor = UIColor.clear
        self.addSubview(boardTable)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("MainViewis not NSCoding compliant")
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.items.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
        cellLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        cellLabel.textAlignment = .center
        cellLabel.text = self.items[indexPath.item]
        cell.contentView.addSubview(cellLabel)
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
        print("User tapped on item \(indexPath.row)")
    }    
}

我可以使用代码更改单元格背景

let cell = self.boardTable.cellForItem(at: IndexPath(row: 1 , section: 0))
cell?.backgroundColor = UIColor.lightGray

如何更改单元格文本颜色或单元格文本内容(cellLabel)?

在此先感谢您的支持。

/* For text color */

//Default set of colours
cellLabel.textColor = .white


//Color of your choice - RGB components in as float values
cellLabel.textColor = UIColor(red: 100.0, green: 100.0, blue: 100.0, alpha: 1) 

/* For text content */
cellLabel.text = "Hello World"

有两种方法

1) 创建一个自定义 UICollectionviewcell class 并将 cellLabel 作为 属性.

创建自定义 class 说 CollectionViewCell

class CollectionViewCell: UICollectionViewCell { var cellLabel: UILabel!

    override func drawRect(rect: CGRect) { //Your code should go here.
        super.drawRect(rect)

    }

}

要创建自定义单元格,请更改以下函数

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! CollectionViewCell

        cellLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        cellLabel.textAlignment = .center
        cellLabel.text = self.items[indexPath.item]
        cell.contentView.addSubview(cellLabel)
        return cell
    }

要更改 cellLabel 的 属性,请使用以下代码

let cell = self.boardTable.cellForItem(at: IndexPath(row: 1 , section: 0))  as! CollectionViewCell
cell?.backgroundColor = UIColor.lightGray
cellLabel.textColor = .white
cellLabel.textColor = UIColor(red: 100.0, green: 100.0, blue: 100.0, alpha: 1) 
cellLabel.text = "Hello World"

2) 遍历单元格子视图并通过 UILabel class 找到标签,然后更改其 属性.

for view in cell.subviews {
    if let label = view as? UILabel {
        label.textColor = UIColor.red
     }
 }

我建议使用第一个。

试试这个:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
        var colors : [UIColor] = [UIColor.blue , UIColor.red , UIColor.cyan]
        cellLabel.textColor = colors[indexPath.row]

        cellLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        cellLabel.textAlignment = .center
        cellLabel.text = self.items[indexPath.item]
        cell.contentView.addSubview(cellLabel)
        return cell
    }

您暂时可以使用此方法,但理想的方法是 Jasmeet Kaur

中建议的方法
for view in cell.subviews {
    if let lable = view as? UILabel {
        lable.textColor = UIColor.red
     }
 }