NSCollectionViewItem属性始终为零

时间:2018-08-05 09:21:38

标签: swift macos appkit

现在,我正在尝试通过configure方法设置NSCollectionViewItem。它正确设置了所有出口和视图,但我无法设置实例属性。他们总是零。为什么这样?

这是集合数据源

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

    func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.data.count
    }

    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {

        let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "CollectionViewItem"),
                                           for: indexPath) as! CollectionViewItem

        item.configure(self.data[indexPath.item], number: indexPath.item + 1)
        return item
    }
}

和物品类别

class CollectionViewItem: NSCollectionViewItem
{
    @IBOutlet weak var numberLabel: NSTextField!

    var data: String?

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

    func configure(_ data: String, number: Int) {

        self.numberLabel.integerValue = number // It's ok, set properly
        self.data = data // Here is the trouble, self.data always nil
    }
}

0 个答案:

没有答案