swiftcocoanscollectionview

Swift: NSCollectionViewItem not showing in NSViewController


I am building an app with NSCollectionView and behaviour of NSCollectionView is so strange, sometimes it makes cell's visible and sometimes are not.

@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"))
}

Then applied the function

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

         prepareCollectionView()
    }

Here is extension function for 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)
    }
    
  
}

Here is programmatically cell for 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 not showing the cells and there is a strange behaviour such as when I add a line resourceCollectionView.reloadData() it shows up and then hides all cells automatically.

What is the missing points for the situation?

Thanks in advance

Edit:

I've solved my problem, when I show another NSViewController I used

self.view = anotherViewController.view 

then no functions of NSCollectionView is triggered.

Then I changed it to

self.view.window?.contentViewController = anotherViewController

Then it worked.


Solution

  • I've solved my problem, when I show another NSViewController I used

    self.view = anotherViewController.view 
    

    then no functions of NSCollectionView is triggered.

    Then I changed it to

    self.view.window?.contentViewController = anotherViewController
    

    It solved my problem.