UICollectionView中的自定义单元格内容

时间:2018-03-21 00:12:20

标签: ios swift swift3 uicollectionview uicollectionviewcell

我正在尝试以编程方式在swift类中添加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)?

提前感谢您的支持。

4 个答案:

答案 0 :(得分:1)

试试这个:

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
    }

答案 1 :(得分:0)

/* 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"

答案 2 :(得分:0)

有两种方法可以做到这一点

1)创建一个自定义UICollectionviewcell类,并将cellLabel作为属性。

创建一个coustom类,说CollectionViewCell

class CollectionViewCell:UICollectionViewCell {         var cellLabel:UILabel!

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

    }

}

要创建custon单元格,请更改以下函数

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类查找标签,然后更改其属性。

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

我建议使用第一个。

答案 3 :(得分:0)

暂时你可以使用这个,但理想的方法是Jasmeet Kaur在answer中建议的方法

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