gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:45:23 31 4
gpt4 key购买 nike

上下文-

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

现在, Collection View 布局的固定高度为 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 上挣扎。我有一种感觉,因为这些项目尚不存在(并且是在 View 开始显示它们时创建的),但不确定如何正确地处理这种动态大小调整。

最佳答案

我也遇到同样的问题。通过阅读文档,我发现 Apple 有意让我们使用 NSCollectionViewLayout 而不是 item。

如果你不知道如何处理NSCollectionViewLayout,你可以尝试使用你的数据模型来重新生成 View 并计算。

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

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

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

关于swift - NSCollectionView sizeForItemAt 在尝试引用其集合中的项目时始终返回异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46286020/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com