NSCollectionView sizeForItemAt 在尝试引用其集合中的项目时始终返回异常

2024-03-11

语境 -

我正在开发一个 macOS 菜单栏应用程序,它显示一系列项目。这些项目存在于集合视图中并包含一个文本字段。

现在,集合视图布局的固定高度为 50,我正在尝试根据文本字段的大小动态增加单元格的大小。

我相信做到这一点的最好方法是sizeForItemAt方法,但我似乎无法引用单元格及其文本字段来计算并返回它的高度。每当我尝试获取一个collectiomView项目时,应用程序就会崩溃并显示[General] *** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]例外。

我的代码 -

我的 viewDidLoad,我在其中“配置”我的 CollectionView

override func viewDidLoad() {
    super.viewDidLoad()

    configureCollectionView()
    ....
}

private func configureCollectionView() {
    let flowLayout = NSCollectionViewFlowLayout()

    flowLayout.itemSize.width = self.view.frame.width
    flowLayout.minimumLineSpacing = 8
    thoughtsCollectionView.collectionViewLayout = flowLayout
    view.wantsLayer = true
}

这是我尝试动态设置高度的地方(也是它崩溃的地方)

func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {

    guard collectionView.item(at: indexPath) != nil else { // <-- CRASHES HERE
        return NSSize(width: self.view.frame.width, height: 50)
    }

    return self.collectionView.item(at: indexPath)?.textField?.frame.height
}

这就是我向 collectionView 添加项目的方式

func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {

    let item = collectionView.makeItem(withIdentifier: "CollectionViewItem", for: indexPath as IndexPath)
    guard let collectionViewItem = item as? CollectionViewItem else {return item}

    (item as! CollectionViewItem).textField?.stringValue = "Some Text"

    (item as! CollectionViewItem).delegate = self

    return item
}

所以对我来说奇怪的是,在我的collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>)我要通过以下方式成功获得该物品

func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {

    guard let indexPath = indexPaths.first else {
        return
    }

    guard collectionView.item(at: indexPath) != nil else {
        return
    }

    let item = collectionView.item(at: indexPath) as! CollectionViewItem // <--- This gets me an item fine

所以我不确定为什么我在挣扎sizeForItemAt。我有一种感觉,因为这些项目尚不存在(并且是在视图开始显示它们时创建的),但不确定如何正确地处理这种动态大小调整。


我遇到同样的问题。通过阅读文档,我发现苹果有意让我们使用NSCollectionViewLayout而不是该项目。

如果你不知道如何应对NSCollectionViewLayout,您可以尝试使用您的数据模型重新生成视图并计算。

例如,如果您有一张图像、一张可变尺寸标签和一张固定尺寸标签。喜欢: 图像 |可变标签 |标签

您可以像这样重新生成可变标签。

func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {

    let imageViewWidth:CGFloat = 20 + 64 + 20
    let account = self.dataObject[indexPath]
    let defaultLabelWidth:CGFloat = {
        if account.isDefault {
            let defaultLabel = NSTextField(labelWithString: NSLocalizedString("Default", comment: ""))
            defaultLabel.font = NSFont.systemFont(ofSize: 16.0)
            defaultLabel.sizeToFit()

            return 20 + defaultLabel.bounds.width + 20
        }

        return 0 + 0 + 20
    }()

    let nameLabelWidth:CGFloat = {
        let label = NSTextField(labelWithString: account.name)
        label.font = NSFont.systemFont(ofSize: 16.0)
        label.sizeToFit()

        return label.bounds.width
    }()

    return NSSize(width: imageViewWidth + nameLabelWidth + defaultLabelWidth, height: 80)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NSCollectionView sizeForItemAt 在尝试引用其集合中的项目时始终返回异常 的相关文章

随机推荐