【问题标题】:NSCollectionView dataSource not working properlyNSCollectionView 数据源无法正常工作
【发布时间】:2016-06-05 20:21:27
【问题描述】:

我正在试验 El Capitan 中引入的新 NSCollectionView API。

在观看 WWDC 视频之后,我创建了一个 NSCollectionViewFlowLayout 的子类来确定集合视图的布局。

class Layout : NSCollectionViewFlowLayout {

  override func prepareLayout() {

    super.prepareLayout()

    self.minimumInteritemSpacing = 0
    self.minimumLineSpacing = 0

    let cheight = self.collectionView!.bounds.height
    let cwidth = self.collectionView!.bounds.width

    self.itemSize = CGSizeMake(cwidth / 2.0, cheight / 6.0)

  }

}

之后,我创建了一个NSObject 子类作为数据源。

class DataSource : NSObject, NSCollectionViewDataSource {

  func collectionView(collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
  }

  func collectionView(collectionView: NSCollectionView, itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem {

    /* DOESN'T GET CALLED! */

    let item = collectionView.makeItemWithIdentifier("cell", forIndexPath: indexPath)

    item.view.wantsLayer = true
    item.view.layer?.backgroundColor = NSColor.redColor().CGColor

    return item

  }

}

问题collectionView:itemForRepresentedObjectAtIndexPath: 永远不会被调用。

这是我初始化集合视图的方式:

let collectionView = NSCollectionView(frame: view.bounds)

let dataSource = DataSource()
let layout = Layout()

collectionView.collectionViewLayout = layout
collectionView.registerClass(NSCollectionViewItem.self,     
                             forItemWithIdentifier: "cell")
collectionView.dataSource = dataSource

collectionView.backgroundColors = [.blackColor()]

我可以在它的superview中清楚地看到collection view,但是没有cell。

此外,如果在委托之外调用此行(但在单元类注册后),则会导致应用崩溃!

let item = collectionView.makeItemWithIdentifier("cell", forIndexPath: /*any index path*/)

是我做错了什么还是 NSCollectionView 新的 API 坏了?

谢谢。

【问题讨论】:

  • 这可能是一个愚蠢的问题,但您正在将集合视图添加到视图的子视图中,对吧?
  • @Aaron 我是。我可以在其超级视图中看到集合视图(没有单元格)
  • 你怎么知道这个方法没有被调用?您是否在该方法中设置了一些断点?或者试图在方法体内记录一些东西?你的cell.xib 文件在同一个包中吗?还是您以编程方式创建接口(因为您注册了 class 而不是 nib)?您是否将集合视图的流形式 "legacy" 更改为例如IB中的“流”
  • @holex 我都试过了。没有 .XIB。
  • @MatteoPacini,你的项目中有 zero .xib 文件吗?

标签: swift macos cocoa


【解决方案1】:

makeItemWithIdentifier 方法很重要,它会在你的包中寻找名为“cell”的 .xib:如果它来自另一个,你可能需要用 registerNib:forItemWithIdentifier: 注册它捆绑。

因此,您的问题出现在此之前,因为您的 collectionView:itemForRepresentedObjectAtIndexPath 永远不会被调用,这可能源于您的模式:为什么要创建一个新类作为数据源而不使用相同的 NSViewController NSCollectionView 驻留在哪里?

我总是这样做,并且使用新的 API 可以完美运行。希望对您有所帮助!

【讨论】:

  • 我正在尝试以编程方式创建集合视图,这就是我使用registerClass:forItemWithIdentifier: 而不是registerNib:forItemWIthIdentifier 的原因。
  • 我也试过 registerClass 和 registerNib 没有运气。 numberOfItemsInSection 被调用,但 itemForRepresentedObjectAtIndexPath 没有。
【解决方案2】:

我遇到了几乎相同的问题,这是我的解决方法。

首先我遇到的问题:

  • 当我的集合视图委托没有实现 NSCollectionViewDelegateFlowLayout 协议时,一切都很好,并且调用了 collectionView:itemForRepresentedObjectAtIndexPath 方法
  • 当我的集合视图委托实现 NSCollectionViewDelegateFlowLayout 协议时,collectionView:itemForRepresentedObjectAtIndexPath 方法未被调用

我注释掉了委托中的每个 NSCollectionViewDelegateFlowLayout 方法,结果发现如果方法 collectionView:layout:referenceSizeForHeaderInSection: 被注释掉了,那么一切都很好(尽管我不使用标题)。

原来 collectionView:layout:referenceSizeForHeaderInSection: 返回一个 NaN 而不是 0.0 作为尺寸的宽度(愚蠢的复制/粘贴错误)。

您确定您的布局返回的内容没有同样的问题吗?

【讨论】:

    【解决方案3】:

    我遇到的问题是我没有为集合视图的项目注册 nib 或类。症状与您描述的完全一样——numberOfItemsInSection 被调用,itemForRepresentedObjectAtIndexPath 未被调用。注册一个 nib 后,该方法开始被调用。这是相关的答案——https://stackoverflow.com/a/39516035/4635852

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多