2017-09-03 58 views
2

我的應用程序需要幾個CollectionViews,因此我決定以編程方式創建它們。我爲每個單元添加了一個UILabel,但是UILabel僅添加到隨機cell s,而其他單元不顯示任何標籤。以編程方式將UILabel添加到CollectionViewCells

如果我上下滾動文本標籤隨機在特定單元格內消失並重新出現在其他單元格上。 這裏的問題是

的視覺描繪(注意標籤消失而我向下滾動)

我需要所有單元,以顯示textLabels。

這是我使用的代碼:

視圖控制器

override func viewDidLoad() { 
    super.viewDidLoad() 


    let frame =     CGRect(x: 0 , y: 50 , width: 375 , height: 300) 
    let layout =     UICollectionViewFlowLayout() 
    let collectionView =   UICollectionView(frame: frame , collectionViewLayout: layout) 
    collectionView.delegate = self 
    collectionView.dataSource = self 
    collectionView.register(CustomCell.self, forCellWithReuseIdentifier: NSStringFromClass(CustomCell.self)) 
    view.addSubview(collectionView) 
} 



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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell =    collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(CustomCell.self), for: indexPath) as! CustomCell 
    cell.nameLabel.text = "XYZ" 
    return cell 

} 

II自定單元

class CustomCell: UICollectionViewCell { 

var nameLabel: UILabel! 

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

    nameLabel =     UILabel(frame: self.frame) 
    nameLabel.textAlignment = .left 
    nameLabel.textColor =  .black 
    contentView.addSubview(nameLabel) 
    contentView.backgroundColor = .red 
    } 

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

    } 

是什麼原因造成ü異常行爲?我是否錯誤地設置了我的collectionView

+0

func CollectionView(_ CollectionView:UICollectionView,heightForRowAt indexPath:IndexPath) - > CGFloat {。這是什麼?這是如何返回UICollectionViewCell的大小https://developer.apple.com/documentation/uikit/uicollectionviewdelegateflowlayout/1617708-collectionview – 2017-09-03 12:13:21

+0

我很抱歉,這是一個錯字。我更新了代碼。 – 6994

+0

以編程方式創建UILabel需要您設置IB爲您所做的所有屬性,因此請參閱:https://stackoverflow.com/questions/17813121/programmatically-creating-uilabel另外,您是否嘗試過UILabel到前面? 第二個問題,看起來像你的出隊問題,嘗試設置awakeFromNib方法內的標籤。 – OhadM

回答

2

您在這一行中做錯了nameLabel = UILabel(frame: self.frame),實際上您給出的位置與收集視圖單元格的位置(x和y座標)相同,這是錯誤的。你需要給nameLabel的位置x = 0和y = 0,那麼它會正常工作。定義nameLabel的幀如下:

var nameLabel: UILabel = UILabel(frame: CGRect.zero) 

override init(frame : CGRect) { 
    super.init(frame : frame) 
    nameLabel.frame.size = CGSize(width: self.frame.width, height: self.frame.height) 
} 

輸出如下:

enter image description here

+0

感謝您的解決方案。這是一個非常粗心的錯誤。 – 6994

1

它看起來像一個出隊問題,因爲它發生在單元出隊期間。

你應該初始化的UILabel的awakeFromNib方法中 - 每細胞被離隊,您將創建你所需要的標籤時這樣

此外,由於您是以編程方式創建UILabel,因此您應該查看此answer,基本上,當您以編程方式創建UILabel時,應該設置更多的值。

我也建議在創建標籤時使用bringSubViewToFront

+0

感謝您的回答。我不知道'awakeFromNib'會如何使用,因爲我不使用'InterfaceBuilder'。另外我嘗試在'cellForItemAtIndexPath'方法中使用'bringSubviewToFront',但那也沒有效果。 – 6994

+0

只需重寫awakeFromNib並將代碼重構爲awakeFromNib - 對故事板和筆尖都有好處。如果你仍然堅持,你可以在單元格內創建一個設置方法(重構你的UILabel代碼)並在cellForRow中出列後調用它,但這是一個糟糕的做法 – OhadM

+0

我以編程方式做所有事情,因此既沒有故事板也沒有可擦寫。 – 6994

0

的問題是,每個小區具有不同值frame.origin因此設定UILabel(frame : self.frame)將是不正確。

例如:

let label = UILabel(frame: self.frame) 
    print(self.frame)      // (130.0, 420.0, 50.0, 50.0) 

// the origin of the label is now CGPoint(130.0, 420.0) with respect to the 
//cell it would be totally out of bounds somewhere down below.    

的每個UILabel需要的originCGPoint(x :0,y :0),使得其正好出現在其各自小區的頂部。

因此,簡單的解決方案是用邊界替換幀。

let label = UILabel(frame: self.bounds) 
相關問題