2016-08-05 12 views
0

我試圖顯示文本是獲取從一個地方到是顯示在我的細胞就像這樣:文字的UILabel內細胞不能完全顯示

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reviewCellID, forIndexPath: indexPath) as! ReviewCell 

    if let comment = selectedCompany.reviews?[indexPath.row].comment { 
     cell.commentLabel.text = comment 
    } 

    return cell 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

    if let comment = selectedCompany.reviews?[indexPath.row].comment { 
     let height:CGFloat = estimateFrameForText(comment).height 
     return CGSizeMake(collectionView.frame.width - 12, height + 70) 
    } 
} 

func estimateFrameForText(text: String) -> CGRect { 
    let size = CGSize(width: collectionView.frame.width - 36, height: 1000) 
    let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin) 
    return NSString(string: text).boundingRectWithSize(size, options: options, attributes: [NSFontAttributeName : UIFont(name: "Helvetica Neue", size: 12.0)!], context: nil) 
} 

的問題是,不是所有的文本顯示。有時候單元格內的文本可能會很長。因此,一旦用戶向上和向下滾動collectionview,它最終不會顯示某些單元格中的所有文本。見下面其中兩個細胞內的文本是完全相同的

enter image description here

我從來沒有使用一個緩存,但我想我需要在這裏使用一個這樣的文本在細胞內正確加載。我可能是錯的。

+1

'comment = selectedCompany ...'甚至編譯?不應該是'如果讓評論=選擇公司...'? –

+0

對不起,我會修復它 – luke

+0

你可以圍繞'cell.titleLabel'繪製邊框嗎?那樣我們就會知道它的框架是否正確。要繪製邊框:'cell.titleLabel.layer.borderWidth = 1',然後'cell.titleLabel.layer.borderColor = UIColor.blueColor()' –

回答

1

問題解決了。我所需要做的就是將UILabel更改爲UITextView

0

確保您在單元格中實施了「prepareForReuse」以清除標籤中的任何以前的文字。如果它不是舊文本,我會向文本標籤圖層添加邊框以檢查它是否有約束問題。我希望它有幫助。