NSCollectionView单选不起作用,但多项选择很好

时间:2017-01-20 03:05:01

标签: macos nscollectionview

如标题所述, 单个选择时,我的NSCollectionView无效。

MyNSCollectionView正确呈现,

下面的代码显示了我如何初始化我的NSCollectionView:

    self.leftBar.dataSource = self
    self.leftBar.delegate = self

    self.leftBar.isSelectable = true
    self.leftBar.allowsEmptySelection = true

    let layout = NSCollectionViewFlowLayout()
    layout.scrollDirection = .horizontal

    layout.itemSize = NSSize(width: 200 , height: 50)

    leftBar.collectionViewLayout = layout


    self.contentView.addSubview(leftBar)

    leftBar <- [
        Top(),
        Left(),
        Bottom(),
        Width(200)
    ]

自定义NSCollectionViewItem

class LeftBarCell: NSCollectionViewItem {

var leftBarView : LeftBarView?

override func loadView() {
    leftBarView = LeftBarView(frame: NSZeroRect)
    view = leftBarView!
}

func setup(title : String){

    leftBarView?.titleTextView.string = title
}

}

在LeftBarView中

class LeftBarView: NSView {

lazy var titleTextView : NSTextView = {

    let titleTextView = NSTextView()

    titleTextView.isEditable = false
    titleTextView.isSelectable = false
    titleTextView.font = NSFont(name: "Helvetica", size: 20)
    return titleTextView
}()

override init(frame frameRect: NSRect) {
    super.init(frame: frameRect)

    self.setupViews()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
}

func setupViews(){
    self.addSubview(titleTextView)

    self.wantsLayer = true
    self.layer?.backgroundColor = NSColor.lightGray.cgColor

    titleTextView <- Edges()
}

}

我试着玩

    self.leftBar.isSelectable = true
    leftBar.allowsMultipleSelection = true

并且多项选择正在进行中。 来自didSelectItemsAt的{​​{1}}被触发

但是当我尝试这个

NSCollectionViewDelegate

    self.leftBar.isSelectable = true

单击collectionviewitem时无法正常工作 <{1}}中的 self.leftBar.isSelectable = true self.leftBar.allowsEmptySelection = true 未被触发。

任何想法都会受到赞赏,谢谢!

1 个答案:

答案 0 :(得分:0)

解决。这是因为textview涵盖了单元格,你可能需要禁用textview变为被动。

extension NSTextView {

 open override func hitTest(_ point: NSPoint) -> NSView? {
    return nil
   }
}