我有幾個UICollectionViewCells,我想以編程方式將兩個標籤添加到單元格中。我如何以正確的方式來做到這一點?如何在Swift 3中以編程方式向UICollectionViewCell添加UILabel
回答
在您的單元格類中將其定義爲全局。
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)
}
}
我想要的標籤添加到UICollectionViewCells不認爲 – fellowProgrammer
這是UICollectionViewCells已經添加它,我將分享所有的代碼,這將是有意義的 –
你可以用關鍵字在Google上:customize table view cell
這是你如何做到這一點:首先,你需要創建的UICollectionViewCell
一個子類,像這樣
:
//我「將使用YourCellClass
爲您custom cell's class
class YourCellClass: UITableViewCell {
// Your can add your label to this Cell
// Review @Özgür Ersil's code above
}
然後,你必須註冊這個類到你的表格視圖。此類必須有標識符
tableView.register(YourCellClass.self, forCellReuseIdentifier: "cellId")
然後設置您的表視圖的datasource
。而在tableView:cellForRowAtIndexPath
添加以下代碼來獲取你的類的實例,並返回它爲您的表視圖行
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId") as! YourCellClass
return cell
這是爲了創建UITableViewCell,而不是將UILabel添加到UICollectionViewCell。 – Azam
- 1. Swift 3以編程方式創建UILabel並添加NSLayoutConstraints
- 2. 以編程方式將UILabel添加到UICollectionViewCell
- 3. 以編程方式在UICollectionViewCell SWIFT中添加多個按鈕+接收touchUpInside事件
- 4. 在UICollectionViewCell中以編程方式添加UIButton不起作用
- 5. 如何以編程方式在swift 3.0中添加UISegmentedControl?
- 6. 如何以編程方式創建UICollectionViewCell
- 7. 如何以編程方式在Swift中更改UIWebView方向
- 8. 如何在Swift中以編程方式旋轉方向?
- 9. 如何編程添加一個UILabel到我的UICollectionViewCell
- 10. 以編程方式向UIViewController添加UITableView
- 11. 以編程方式向UINavigationBar添加UIBarButtonItem
- 12. 使用swift以編程方式向UITableView添加標題2.2
- 13. 使用Swift以編程方式向按鈕添加約束
- 14. 以編程方式向UIImageView添加UIImage
- 15. 以編程方式向UINavigationController添加UIButton
- 16. 以編程方式向列添加列
- 17. 如何在Swift中向UICollectionViewCell添加背景圖片?
- 18. 如何使用SWIFT以編程方式設置UICollectionViewCell大小,取決於其中的UIlabel的動態大小?
- 19. Swift 3:以編程方式切換ViewControllers
- 20. Swift:如何安排以編程方式添加的複選框?
- 21. Swift 3:以編程方式調整UICollectionViewCell的大小導致單元格重疊
- 22. 如何以編程方式向SiteCore中的GeneralField添加值
- 23. 如何以編程方式向Gridview中的Textview添加權重
- 24. 如何以編程方式向Word文檔添加樣式
- 25. 以編程方式在Swift中添加UIBarButtonItem動作
- 26. IOS以編程方式在Swift中添加約束條件
- 27. 以編程方式在swift中添加「垂直間距」
- 28. 在SWIFT OS X中以編程方式添加和刪除NSTextField
- 29. 以編程方式在swift中添加全尺寸視圖
- 30. Swift:以編程方式在鍵盤上方添加UIButton
從的cellForRowAtIndexPath –