如何以编程方式将UILabel和UIImageView添加到CollectionView?

时间:2018-09-20 16:55:45

标签: swift

我已经以编程方式创建了一个集合视图,并且无法弄清楚如何在单元格中添加标签和背景图片。这是我到目前为止用于集合视图的代码。

import UIKit

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    let cell = "cellId"
    let text = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "Vote"
        collectionView?.backgroundColor = UIColor.white
        collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cell)

    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cell, for: indexPath)
        cell.backgroundColor = UIColor.lightGray
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: 200)
    }
}

2 个答案:

答案 0 :(得分:0)

一个简单的自定义UICollectionViewCell类如下所示:

class MyCell : UICollectionViewCell {
    var label1: UILabel
    var label2: UILabel
    var bgImg:  UIImageView
}

编辑viewDidLoad函数:

collectionView?.register(MyCell.self, forCellWithReuseIdentifier: cell)

编辑cellForItemAt函数:

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cell, 
                                                              for: indexPath) as! MyCell

cell.bgImg.image = UIImage(named: "img.png")
cell.label1.text = "..."
cell.label2.text = "..."

return cell

使用init类中的MyCell函数来布局标签。

答案 1 :(得分:0)

创建集合视图单元格的子类,并使用CustomCollectionViewCell代替UICollectionViewCell

class CustomCollectionViewCell: UICollectionViewCell {
var imageView: UIImageView?
var label: UILabel? 



override init(frame: CGRect) {
    super.init(frame: frame)

    imageView = UIImageView(frame: self.bounds)
    //customise imageview
    imageView?.backgroundColor = UIColor.red
    contentView.addSubview(imageView!)
    label = UILabel(frame: CGRect(x: 20, y: 20, width: self.bounds.width - 20, height: 20))
    //Customsize label
    label?.text = "Hello"
    label?.textColor = UIColor.white
    contentView.addSubview(label!)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override var bounds: CGRect {
    didSet {
        contentView.frame = bounds
    }
}
}

在View Controller中更新了以下代码:

override func viewDidLoad() {
        super.viewDidLoad()

    navigationItem.title = "Vote"
    collectionView?.backgroundColor = UIColor.white
    collectionView?.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: cell)

}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cell, for: indexPath) as! CustomCollectionViewCell
        cell.backgroundColor = UIColor.lightGray
        return cell
    }