我試圖顯示文本是獲取從一個地方到是顯示在我的細胞就像這樣:文字的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,它最終不會顯示某些單元格中的所有文本。見下面其中兩個細胞內的文本是完全相同的
我從來沒有使用一個緩存,但我想我需要在這裏使用一個這樣的文本在細胞內正確加載。我可能是錯的。
'comment = selectedCompany ...'甚至編譯?不應該是'如果讓評論=選擇公司...'? –
對不起,我會修復它 – luke
你可以圍繞'cell.titleLabel'繪製邊框嗎?那樣我們就會知道它的框架是否正確。要繪製邊框:'cell.titleLabel.layer.borderWidth = 1',然後'cell.titleLabel.layer.borderColor = UIColor.blueColor()' –