2017-02-28 110 views

回答

1

在您的單元格類中將其定義爲全局。

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) 
     } 
} 
+0

我想要的標籤添加到UICollectionViewCells不認爲 – fellowProgrammer

+0

這是UICollectionViewCells已經添加它,我將分享所有的代碼,這將是有意義的 –

-1

你可以用關鍵字在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 
+0

這是爲了創建UITableViewCell,而不是將UILabel添加到UICollectionViewCell。 – Azam

相關問題