我正在用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然后起作用了。
发布于 2020-07-08 21:25:38
我已经解决了我的问题,当我展示我使用的另一个NSViewController时
self.view = anotherViewController.view 然后不触发NSCollectionView的任何功能。
然后我把它改成
self.view.window?.contentViewController = anotherViewController它解决了我的问题。
https://stackoverflow.com/questions/62798519
复制相似问题