【问题标题】:Custom Cell contents in UICollectionViewUICollectionView 中的自定义单元格内容
【发布时间】:2018-08-29 23:19:07
【问题描述】:

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

提前感谢您的支持。

【问题讨论】:

  • 如果您能够更改单元格背景颜色,请尝试访问您在单元格上添加的标签并更改其文本颜色,例如 cell?.cellLabel.textColor = UIColor.lightGray
  • 如果我使用 cell?.cellLabel.textColor = UIColor.lightGray,我得到了那个错误:'UICollectionViewCell' 类型的值没有成员'cellLabel'
  • 是的,cellLabel 不是 collectionViewCell 的成员,而是 BoardView 类的成员,理想情况下您应该能够使用 self 访问它。所以请尝试 self.cellLabel.textColor = UIColor.lightGray
  • 它没有用。它仅在最后一个单元格文本处更改颜色
  • cell.contentView.addSubview(cellLabel) 不要,根本不要。细胞被重复使用。使用自定义 UICollectionViewCell 和您可能访问的自己的初始化/属性。

标签: ios swift swift3 uicollectionview uicollectionviewcell


【解决方案1】:

有两种方法

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

创建一个自定义类,比如 CollectionViewCell

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

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

我建议使用第一个。

【讨论】:

  • 我是 swift 新手,所以我不知道怎么做
  • 谢谢。我改变了我的方法。我在创建表格时写了一个自定义单元类类 MyCollectionViewCell: 之后我将自定义类附加到表格 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseIdentifier, for: indexPath) 为! MyCollectionViewCell 现在它可以工作了
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    试试这个:

    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
        }
    

    【讨论】:

    • 感谢您的重播。我的问题是:创建表格后如何更改单元格文本。
    【解决方案4】:
    /* 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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多