ios - UICollectionViewCell 的可变宽度和高度使用 AutoLayout 和 Storyboard(没有 XIB)

标签 ios uicollectionview autolayout storyboard height

这是应用程序中的一个设计问题,它使用 AutoLayout、UICollectionView 和 UICollectionViewCell,它们根据 AutoLayout 约束及其内容(一些文本)自动调整宽度和高度。

这是一个 UITableView 列表,每个单元格都有自己的宽度和高度,根据其内容为每一行单独计算。它更像是在应用程序(或 WhatsUp)中构建的 iOS 消息。

很明显,应用程序应该使用 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize

问题是在该方法中,应用程序无法调用 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCelldequeueReusableCellWithReuseIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath! ) -> AnyObject 实例化单元格,用特定内容填充它并计算它的宽度和高度。尝试这样做会导致无限期的递归调用或某种其他类型的应用程序崩溃(至少在 iOS 8.3 中)。

解决这种情况的最接近的方法似乎是将单元格的定义复制到 View 层次结构中,让自动布局自动调整“单元格”的大小(比如单元格与父 Collection View 具有相同的宽度),因此应用程序可以配置单元格具体内容并计算其大小。由于重复的资源,这绝对不是修复它的唯一方法。

所有这些都与将 UILabel.preferredMaxLayoutWidth 设置为某个值有关,该值应该是自动布局可控的(不是硬编码的),这可能取决于屏幕宽度和高度,或者至少由自动布局约束定义设置,因此应用程序可以获取计算多行 UILabel 固有大小。

我不想从 XIB 文件中实例化单元格,因为 Storyboard 应该是当今的行业标准,我希望对代码的干预尽可能少。

编辑:

无法运行的基本代码如下所示。因此,仅实例化变量 cellProbe(未使用)会使应用程序崩溃。没有那个电话,应用程序运行顺利。

var onceToken: dispatch_once_t = 0

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

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

        dispatch_once(&onceToken) {

            let cellProbe = collectionView.dequeueReusableCellWithReuseIdentifier("first", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! UICollectionViewCell
        }

        return CGSize(width: 200,height: 50)
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("first", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! UICollectionViewCell

        return cell
    }
}

最佳答案

如果您使用约束,则无需为每个单元格设置大小。所以你应该删除你的委托(delegate)方法

相反,您需要在 estimatedItemSize 中设置一个大小,方法是将其设置为 CGSizeZero 以外的值,您是在告诉布局您还不知道确切的大小.然后,布局会询问每个单元格的大小,并在需要时进行计算。

let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.estimatedItemSize = someReasonableEstimatedSize()

关于ios - UICollectionViewCell 的可变宽度和高度使用 AutoLayout 和 Storyboard(没有 XIB),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30998480/

相关文章:

ios - 使用自动布局(snapkit)的循环 View ?

cocoa - 对图层支持的 View 进行动画处理时缩放图层内容

ios - 是什么导致具有相同自动布局约束的两个 UITableViewCells 表现不同?

iphone - 声明说明 - (IBAction)changeGreeting :(id)sender; iPhone

ios - 使用 Storyboard从 UITableviewcell 移动到 ViewController 并返回

ios - 我可以在 Xcode 项目中使用 SVG 文件吗?

ios - 是否可以增加 UICollectionViewLayout 视口(viewport)? (即在 UICollectionView 框架外加载 +2/-2 个单元格)

ios - 在 UICollectionView 单元格中停止动画

ios - Unity3d UI 事件在 iOS 设备上不起作用

ios - UICollectionViewFlowLayout - 强制为某些项目换行