Swift: NSCollectionViewItem 未显示在 NSViewController 中

Swift: NSCollectionViewItem not showing in NSViewController

我正在使用 NSCollectionView 构建应用程序,NSCollectionView 的行为非常奇怪,有时它使单元格可见,有时不可见。

@IBOutlet weak var resourceCollectionView: NSCollectionView!

private func prepareCollectionView() {
    let flowLayoutForEvent = NSCollectionViewFlowLayout()
flowLayoutForEvent.scrollDirection = .vertical
flowLayoutForEvent.minimumInteritemSpacing = 100
       
resourceCollectionView.collectionViewLayout = flowLayoutForEvent
       
resourceCollectionView.delegate = self
resourceCollectionView.dataSource = self
resourceCollectionView.isSelectable = true
       
       
resourceCollectionView.backgroundColors = [.clear]
resourceCollectionView.register(SimpleCell.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "SimpleCell"))
}

然后应用函数

public override func viewDidLoad() {
        super.viewDidLoad()

         prepareCollectionView()
    }

这是NSCollectionView

的扩展函数
extension DashboardController : NSCollectionViewDelegateFlowLayout, NSCollectionViewDataSource {
  
  // 1
  public func numberOfSections(in collectionView: NSCollectionView) -> Int {
    return 1
  }
  
  // 2
  public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
  }
  
  // 3
  public func collectionView(_ itemForRepresentedObjectAtcollectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {    
    let cell = resourceCollectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "SimpleCell"), for: indexPath) as! SimpleCell

   
     return cell

  }
    
    public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
        print("selected item > ",  indexPaths )
    }
    
    
    public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
        return NSSize(width: 10 , height:10)
    }
    
  
}

这是 NSCollectionView

的编程单元格
class SimpleCell: NSCollectionViewItem {

private var containerView: NSView!

private var containBox: NSBox = {
    var box = NSBox()
    box.boxType = .custom
    box.fillColor = NSColor.purple
    box.translatesAutoresizingMaskIntoConstraints = false
    return box
}()


override func loadView() {
    containerView = NSView()
    self.view = containerView
    containerView.bounds = self.view.bounds


    containerView.addSubview(containBox)
    containBox.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
    containBox.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
    containBox.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
    containBox.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
}


}

resourceCollectionView 不显示单元格并且有一个奇怪的行为,例如当我添加一行时 resourceCollectionView.reloadData() 它会显示然后自动隐藏所有单元格。

情况的缺失点是什么?

提前致谢

编辑:

我已经解决了我的问题,当我展示另一个NSViewController 我用

self.view = anotherViewController.view 

那么没有触发NSCollectionView的功能

然后我改成了

self.view.window?.contentViewController = anotherViewController

然后成功了。

我已经解决了我的问题,当我展示我使用的另一个 NSViewController 时

self.view = anotherViewController.view 

则没有触发NSCollectionView的功能

然后我改成了

self.view.window?.contentViewController = anotherViewController

它解决了我的问题。