- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
上下文-
我正在开发一个 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/
我在 UITableViewCell 中有 UICollectionView ,它的委托(delegate)和数据源在表格单元类中声明。它第一次加载时效果很好,但后来当我滚动集合时会更改它的大小。我注
上下文- 我正在开发一个 macOS 菜单栏应用程序,它显示一系列项目。这些项目存在于 Collection View 中并包含一个文本字段。 现在, Collection View 布局的固定高度为
我希望第一部分的单元格为 Collection View 的全宽度,第二部分的单元格为宽度的一半。启动时,单元格不遵循给定的宽度,尽管高度似乎受到尊重。几秒钟后, Collection View 将重
我正在尝试根据图片的纵横比以及其他因素来动态调整单元格的大小,以创建自定义布局。这些计算似乎减慢了我的应用程序的加载速度。我想将此进程移至后台线程。下面是该函数的代码。是否可以在后台线程上运行此函数以
我正在为我的 Collection View 使用自定义布局,但如果我使用自定义布局,sizeForItemAt 方法不会调用。 我的自定义布局: public class HorizontalCol
我正在尝试根据里面的内容设置我的单元格的大小这是我的代码,它崩溃了,我找不到错误消息。 extension TimelineCollectionVC: UICollectionViewDelegate
我想给所有单元格大小以适合所有设备。这是索引路径中我的 Collection View 单元格 sizeForItem 的代码: extension PersonListViewController:
我正在使用 sizeForItemAt 设置一个 View Controller 的单元格大小,但似乎我也必须为其他 CollectionView 返回一个结果,问题是我没有其他 Collection
我正在尝试在 Collection View 中设置单元格大小变化的动画。 在“didSelectItemAt”中: collectionView.performBatchUp
我的 UICollectionView 的宽度/高度匹配所有可用空间。 我想在一行(两列)中显示两个单元格 所以一个单元格的宽度应该是UICollectionView的一半宽度(collectionV
我正在尝试布局我的 collectionViewCell 的大小,以便每行中有 7 个单元格(很简单吧?)。 cell很简单,只有一个UILabel,上、左、下、右约束均为0。 我已经设置了 size
我在 viewcontroller 中设置了 2 个 collectionView,它们都从端点获取数据,然后 reloadData()。 一个 collectionView 就像一个标题选项卡,其单
我使用自动布局来动态计算 collectionView 单元格的大小。一些单元格在第一次滚动到查看端口时使用来自重用单元格的尺寸。当我继续滚动 collectionView 时,它们将被设置为正确的值
我遇到了一个奇怪的问题,我不确定为什么会发生这种情况。如您所见,这两个图像一个是运行 ipad 的 ios 9,另一个是运行 ipad 的 ios11。对于 ios9,运行 ipad 单元格不会调整大
我有 ViewController,其中有两个 collectionView 但对于一个我想要 isPagingEnabled 用于单元格和另一个 collectionView全帧宽度的 3 项。我怎
我有一个 UICollectionViewCell,里面有一个 UICollectionView 和 UICollectionViewFlowLayout。我正在使单元格成为 Collection V
所以正如标题所暗示的那样,我似乎遇到了一个奇怪的问题。我在这里要做的就是创建一个 2 列的 Collection View ,而无需将任何内容硬编码到我的委托(delegate)方法中。调试后我发现
我有一个 collectionView 设置如下,但是在 sizeForItemAt 中 collectionView!.frame.size 与渲染结果不同。 注意:我只对 collectionVi
我使用 UICollectionView 制作了画廊。 如果你降低屏幕,图像应该继续出现并停在 20。 代码不断更新。但是设置cell大小的函数collectionView(_:layout:size
实现 我想在 UICollectionView 中拥有两种不同的项目大小。因此我已经实现了 UICollectionViewDelegateFlowLayout 的 collectionView(_:
我是一名优秀的程序员,十分优秀!