Ios 集合视图单元格大小取决于UITextView

Ios 集合视图单元格大小取决于UITextView,ios,swift,uicollectionview,height,uicollectionviewcell,Ios,Swift,Uicollectionview,Height,Uicollectionviewcell,我有一个UICollectionView,其中有一个单元格,其中包含一个UITextView。我希望单元格的大小取决于TextView的高度(这样就不需要滚动,所有文本都始终可见)。 我正在尝试: func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSInde

我有一个
UICollectionView
,其中有一个
单元格
,其中包含一个
UITextView
。我希望单元格的大小取决于TextView的高度(这样就不需要滚动,所有文本都始终可见)。 我正在尝试:

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

    if (indexPath.row < myComments!.count) {

        if let messageText = myComments![indexPath.row].text  {
            let size = CGSizeMake(self.view.frame.width / 2, 100)
            let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin)
            let estimatedFrame = NSString(string: messageText).boundingRectWithSize(size, options: options, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14)], context: nil)
            return CGSizeMake(view.frame.width, estimatedFrame.height + 50)
        }
    }
    return CGSizeMake(view.frame.width, 100)

}
func collectionView(collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeFormiteIndepath indepath:nsindepath)->CGSize{
if(indexath.row

然而,它并没有真正起作用。有时高度太大,有时太小,我仍然需要滚动。

使用TextView的sizeThatFits方法。它精确计算UITextView所需的大小