简体   繁体   English

UICollectionView中的自定义单元格内容

[英]Custom Cell contents in UICollectionView

I'm trying to add a UICollectionView in a swift class programmatically, without any storyboard, with a custom cell (a simple label in every cell) 我正在尝试以编程方式在没有任何故事板的情况下以快速方式添加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)")
    }    
}

I'm able to change the cell background with the code 我可以使用代码更改单元格背景

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

How I may change the cell text color or the cell text content (the cellLabel)? 如何更改单元格文本的颜色或单元格文本的内容(cellLabel)?

Thanks in advance for your support. 预先感谢您的支持。

Try This : 尝试这个 :

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

There are two ways of doing it 有两种方法

1) Create a custom UICollectionviewcell class and which have cellLabel as a property. 1)创建一个自定义UICollectionviewcell类,并将其具有cellLabel作为属性。

Create a coustom class say CollectionViewCell 创建一个提示类,例如CollectionViewCell

class CollectionViewCell: UICollectionViewCell { var cellLabel: UILabel! class CollectionViewCell:UICollectionViewCell {var cellLabel:UILabel!

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

    }

} }

And to create the custon cell change the below func 并创建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
    }

To change the property of cellLabel use below code 要更改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) Iterate through cell subviews and find label by UILabel class and then change its property. 2)遍历单元格子视图,并按UILabel类查找标签,然后更改其属性。

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

I would suggest using 1st one. 我建议使用第一个。

For time being you can use this but the ideal approach would be the one suggested in this answer by Jasmeet Kaur 暂时可以使用此方法,但理想的方法是Jasmeet Kaur在此答案中建议的方法

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

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

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