gpt4 book ai didi

swift - NSCollectionViewItem 的动态大小

转载 作者:搜寻专家 更新时间:2023-11-01 05:33:57 29 4
gpt4 key购买 nike

我正在尝试为我的 NSCollectionViewItem 提供动态大小。我希望它们的大小由它们的内容和我定义的约束决定。

为此,我已将以下内容添加到包含 NSCollectionView 的 View Controller 中:

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

let item = collectionView.item(at: indexPath)
if let viewItem = item as NSCollectionViewItem? {
return viewItem.view.fittingSize
}

return NSSize(width: 200, height: 100)

}
}

抛出异常。

An uncaught exception was raised
*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]

我不知道它从哪里来,我试图捕捉它,但 xcode 说 item(at:) 不会抛出异常。

编辑:但事实上确实如此,我有一个异常断点,它在 item(at:) item(at:) exception 中中断

除了向 Apple 发送错误报告之外,还有其他想法吗?并且由于 item(at:) 不应该抛出错误,所以 do catch bloc 是无效的

最佳答案

如您所述,我认为此方法不会崩溃。来自文档:

Return: The item for the specified index path or nil if no item is available.

你试过add an exception breakpoint吗? ?您应该看到它崩溃的地方。

关于swift - NSCollectionViewItem 的动态大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46080381/

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