简体   繁体   English

如何使UICollectionview单元格高度动态?

[英]How to make UICollectionview cell height dynamic?

I have a UIcollectionview . 我有一个UIcollectionview There are some cells of the Collectionview. Collectionview有一些单元格。 Inside that CollectionviewCell there is a textview. CollectionviewCell里面有一个textview。 The textview's content is dynamic. textview的内容是动态的。 So the cell height should be dynamic. 因此,细胞高度应该是动态的。

How to do that? 怎么做?

1-Add UICollectionViewDelegateFlowLayout protocol 1 - 添加UICollectionViewDelegateFlowLayout协议
2-Conform to protocol by implement the sizeForItemAtIndexPath method in your ViewController 2 - 通过在ViewController中实现sizeForItemAtIndexPath方法来符合协议
3-By the index of the cell get the text you have and caculate the hight and width of it 3 - 通过单元格的索引获取您拥有的文本并计算它的高度和宽度

class ViewController: UIViewController,UICollectionViewDelegateFlowLayout{

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let constraintRect = CGSize(width: self.view.frame.size.width, height: CGFloat.max)
        let data = myTextArray[indexpath.row];
        let boundingBox = data.boundingRectWithSize(constraintRect, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: Constants.strings.fontName, size: 30)!], context: nil)
        return CGSizeMake(boundingBox.width, boundingBox.height); //(width,hight)
}

}

Use NSAttributedString in sizeForItemAtIndexPath to calculate rect for the height and width: sizeForItemAtIndexPath使用NSAttributedString来计算高度和宽度的rect:

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

    let attString = NSAttributedString(string: self.rows[indexPath.row], attributes: [NSFontAttributeName: UIFont.systemFontOfSize(15.0)])
    var r: CGRect = attString.boundingRectWithSize(CGSizeMake(self.collectionView.bounds.size.width, CGFLOAT_MAX), options: .UsesLineFragmentOrigin, context: nil)

    return r.size

}

1) Override collectionflowlayout delegate method 1)覆盖collectionflowlayout委托方法

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(50, 50); //(width,hight)
}

2) In above method, Calculate the height of textview which contains dynamic length and add that value in your fixed height. 2)在上述方法中,计算包含动态长度的textview的高度,并将该值添加到固定高度。 Ex. cellHeight = 100 + textview height. //100 is height which not contain textview height.

3) After calculating height, replace it in above method 3)计算高度后,用上述方法更换

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
            return CGSizeMake(50, dynamicheight); //(width,hight)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM