简体   繁体   English

如何以编程方式在Swift 3中将UILabel添加到UICollectionViewCell

[英]How to programmatically add a UILabel to UICollectionViewCell in Swift 3

I have a few UICollectionViewCells and I want to add two labels to the cells programmatically. 我有几个UICollectionViewCells,我想以编程方式向单元格添加两个标签。 How would I do this the correct way? 我怎么能以正确的方式做到这一点?

In your cell class define it global. 在您的单元类中,将其定义为全局。

class YourCollectionViewCell: UICollectionViewCell {

        var titleLabel:UILabel = {
            let label = UILabel(frame: CGRect(x:100, y: 30, width: UIScreen.main.bounds.width , height: 40))
            label.textAlignment = .left
            label.lineBreakMode = .byWordWrapping
            label.numberOfLines = 0
            return label
        }()

        override init(frame: CGRect) {
            super.init(frame: frame)
            self.addSubview(self.titleLabel)
        }
}

You can google with keyword: customize table view cell 您可以使用关键字google: customize table view cell

This is how you can do it: 这是你如何做到的:

Firstly, You need to create a subclass of UICollectionViewCell like so: 首先,您需要创建一个UICollectionViewCell的子类, UICollectionViewCell所示:

// I'll use YourCellClass as your custom cell's class //我将使用YourCellClass作为custom cell's class

class YourCellClass: UITableViewCell {
// Your can add your label to this Cell
// Review @Özgür Ersil's code above
}

Then, you have to register this class to your table view. 然后,您必须将此类注册到表视图。 This class must have a Identifier 该类必须具有标识符

tableView.register(YourCellClass.self, forCellReuseIdentifier: "cellId")

then set datasource of your table view. 然后设置表视图的datasource And in tableView:cellForRowAtIndexPath add following code to get instance of your cell Class and return it for your table view row 并在tableView:cellForRowAtIndexPath添加以下代码以获取您的单元格类的实例并将其返回给您的表视图行

let cell = tableView.dequeueReusableCell(withIdentifier: "cellId") as! YourCellClass
return cell

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

相关问题 如何以编程方式将UILabel添加到我的UICollectionViewCell - How to programmatically add an UILabel to my UICollectionViewCell 斯威夫特| 如何以编程方式将尾随空间添加到UILabel? - Swift | How to add Trailing Space to UILabel programmatically? 快速,以编程方式更改UICollectionViewCell和UILabel(在单元格内)的宽度 - Swift, Change width of UICollectionViewCell and UILabel(inside the cell) programmatically Swift & UILabel:如何以编程方式在 Swift 中添加填充和边距? - Swift & UILabel : How to add padding and margin in Swift programmatically? 将UIView和UILabel添加到UICollectionViewCell - Add UIView and UILabel to UICollectionViewCell 如何在 Swift 中以编程方式更改 UICollectionViewCell 大小? - How to change UICollectionViewCell size programmatically in Swift? Swift 3以编程方式创建UILabel并添加NSLayoutConstraints - Swift 3 Create UILabel programmatically and add NSLayoutConstraints 在 Swift 4 中以编程方式将 UIImageView、UILabel 添加到 UIStackView - Add UIImageView, UILabel to UIStackView programmatically in Swift 4 如何在UICollectionViewCell中设置UILabel - How to set a UILabel in UICollectionViewCell 如何在UICollectionViewCell上设置UILabel? - How to set UILabel on UICollectionViewCell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM