【问题标题】:Setting text label in UICollectionView在 UICollectionView 中设置文本标签
【发布时间】:2020-06-03 07:31:20
【问题描述】:

我有一个基本的集合视图,它是一个彩色网格。我要做的就是在网格单元格中添加一个文本标签。在我的故事板中,我在单元格的内容视图中拖动了一个标签。我在控制整个集合的视图控制器中为这个标签创建了一个出口。但不知何故,我无法在此视图控制器中设置单元格的文本。我需要做什么?

import UIKit
let reuseIdentifier = "CellIdentifer";

class WordsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    @IBOutlet var collectionView: UICollectionView!
    @IBOutlet weak var textLabel: UILabel!
    @IBOutlet var cellView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //UICollectionViewDelegateFlowLayout methods
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
    {

        return 4;
    }
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
    {

        return 1;
    }


    //UICollectionViewDatasource methods
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

        return 1
    }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return 100
    }

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

        cell.backgroundColor = self.randomColor()

        return cell
    }


    // custom function to generate a random UIColor
    func randomColor() -> UIColor{
        let red = CGFloat(drand48())
        let green = CGFloat(drand48())
        let blue = CGFloat(drand48())
        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }
}

【问题讨论】:

    标签: swift uicollectionview uicollectionviewcell


    【解决方案1】:

    created an outlet for this label inside the view controller 如果你想在每个单元格中都有一个文本,你不能这样做。您想要做的是继承 UICollectionViewCell(例如 CustomCollectionViewCell)。将 Storyboard 中的 collectionViewCell 设置为这个 Class 并连接标签。现在您可以像这样设置文本:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as CustomCollectionViewCell
    
        cell.backgroundColor = self.randomColor()
        cell.myShinyNewTextLabel.text = "myCustomText"
        return cell
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 2012-03-09
      • 1970-01-01
      相关资源
      最近更新 更多