繁体   English   中英

Swift:NSCollectionViewItem 未显示在 NSViewController 中

[英]Swift: NSCollectionViewItem not showing in NSViewController

我正在使用 NSCollectionView 构建一个应用程序,并且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"))
}

然后应用了 function

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

         prepareCollectionView()
    }

这是 NSCollectionView 的扩展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

它解决了我的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM