如何在UICollectionView中基于UILabel子视图调整单元格大小

时间:2014-07-13 04:04:03

标签: ios swift uicollectionview

我所做的是将标签设置为使用label.sizeToFit()调整大小,这很方便,但我现在需要设置单元格以正确调整其大小。

我使用FlowLayoutDelegate中的sizeForIndexPath方法将单元格大小设置为:

messages[indexPath.row].sizeWithAttributes(nil)
然而,这给了我非常薄/小的CGSize。

import UIKit

let reuseIdentifier = "Cell"

class PhotoCollectionViewController: UICollectionViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    var messages = NSString[]()

    override func viewDidLoad() {
        super.viewDidLoad()
        messages = ["Hello, How are you?", "Ldsfsd sdf dsfs s fs fs", "fsdfsdfsdfs fsdfsdfsdf sfs fs", "yoyo yoy oyo", "sdfd sfds fssfs"]

        self.collectionView.registerClass(TextCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        self.collectionView.collectionViewLayout = PhotoCollectionViewFlowLayout()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // #pragma mark UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView?) -> Int {
        return 1
    }


    override func collectionView(collectionView: UICollectionView?, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return messages.count
    }

    override func collectionView(collectionView: UICollectionView?, cellForItemAtIndexPath indexPath: NSIndexPath?) -> UICollectionViewCell? {
        let cell = collectionView?.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as TextCollectionViewCell
        cell.backgroundColor = UIColor.lightGrayColor()
        cell.newLabel.text = messages[indexPath!.row]
        cell.newLabel.sizeToFit()

        return cell
    }

    func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize {
        //println(messages[indexPath.row].sizeWithAttributes(nil))
        return messages[indexPath.row].sizeWithAttributes(nil)
    }
}

编辑:我让它工作,sorta(以一种未经优化的方式):

func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize {
    let cell = TextCollectionViewCell(frame: CGRect(x: 0, y: 8, width: 300, height: 100))
    cell.newLabel.text = messages[indexPath.item]
    cell.newLabel.sizeToFit()

    println(cell.newLabel.intrinsicContentSize())
    //var width = UIWindow.
    return CGSizeMake(cell.newLabel.intrinsicContentSize().width+10, cell.newLabel.intrinsicContentSize().height+20)
    //return cell.newLabel.intrinsicContentSize()
}

现在,文字适用于大多数情况: 当宽度比屏幕尺寸更长时,它不适合,因为显然不会自动启动新行。 它也会显示在屏幕中间,不确定布局中我可以更改此位置或是否依赖于其他内容。

1 个答案:

答案 0 :(得分:1)

如果您想将自行调整单元格与UICollectionViewFlowLayout一起使用,则需要将estimatedItemSize设置为非CGSizeZero尺寸。