如何在swift4中正确实现NSCollectionView?

时间:2018-02-25 16:54:48

标签: swift macos nscollectionview

你可以查看我的代码有什么问题吗?我可以成功运行应用程序但内容空白。谢谢!

import Cocoa

class MainViewController: NSViewController {

    @IBOutlet weak var scrollView: NSScrollView!
    @IBOutlet weak var collectionView: NSCollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        self.initView()
        self.initCollectionView()
    }

    func initView() {
        let nib = NSNib(nibNamed: NSNib.Name(rawValue: "NavCollectionViewCell"), bundle: nil)
        //ßßself.collectionView.register(nib, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "NavCollectionViewCell"))
        self.collectionView.register(nib, forItemWithIdentifier: .collectionViewItem)
    }

    func initCollectionView() {
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
    }
}

extension MainViewController: NSCollectionViewDelegate, NSCollectionViewDataSource {
    func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

    func numberOfSections(in collectionView: NSCollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
        // 4
        let item = collectionView.makeItem(withIdentifier: .collectionViewItem, for: indexPath)
        return item

    }
}

extension NSUserInterfaceItemIdentifier {
    static let collectionViewItem = NSUserInterfaceItemIdentifier("NavCollectionViewCell")
}

0 个答案:

没有答案