2015-10-14 100 views
1

我爲我的collectionview單元實現了一個自定義類。 該類識別標籤的屬性,但是當我運行我的應用時,標籤文本不顯示。 類是這樣註冊:自定義UICollectionViewCell類可以識別但不能工作

self.collectionView!.registerClass(StatsCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 

這裏是自定義單元格類的init方法:

var textlabel = UILabel() 

override init(frame: CGRect) { 
    let width = frame.size.width 
    let height = frame.size.height 
    textlabel = UILabel(frame: CGRectMake(0, 0, width,height)) 
    super.init(frame: frame) 
} 

和實現:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! StatsCollectionViewCell 

    // Configure the cell 
    cell.textlabel.text = "test" 

    return cell 
} 

可能有人點我到我的錯請,我在這裏有點失落,看不出有什麼不對。

請注意,我可以使用單元格屬性(這意味着單元格實際上是在這裏)。

回答

2

你沒有把textlabel置於view

override init(frame: CGRect) { 
    let width = frame.size.width 
    let height = frame.size.height 
    textlabel = UILabel(frame: CGRectMake(0, 0, width,height)) 
    super.init(frame: frame) 
    addSubview(textlabel) // add text label to view 
} 
+0

感謝的人,我一直在這過去一小時我真是個白癡...... 要去得到一些睡眠:d –

相關問題