ios - 如何根据其中的 UILabel subview 调整 UICollectionView 中的单元格大小

标签 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)
    }
}

编辑:我已经让它工作了,有点(以未优化的方式):

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()
}

现在文本适用于大多数情况: 当它的宽度长于屏幕尺寸时它不适合,因为显然新行不会自动开始。 它还显示在屏幕中间,不确定我可以在布局中的哪个位置更改它或者它是否依赖于其他东西。

最佳答案

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

关于ios - 如何根据其中的 UILabel subview 调整 UICollectionView 中的单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24719441/

相关文章:

ios - 在没有动画的情况下呈现 UIViewController 会显示演示者 UIViewController 的时间

ios - 快速打开一个元组

swift - 我如何在 Swift 中将 "6 September, 2015"转换为 NSDate()?

ios - 在 ViewDidLoad 中解析 UICollectionViewController 的查询

ios - UICollectionView 未加载

android - 谷歌应用程序的授权方式是什么?

iphone - Cell TextLabel 多行不再工作

ios - 是否可以在 iOS 中通过 PlaceId 打开谷歌地图?

ios - 如何根据 segmentedControl 索引更改数据

ios - 这个NSFetchRequest有什么问题?