Swift & NSTableView - Simply select a row

杀马特。学长 韩版系。学妹 提交于 2019-12-06 03:01:03

问题


I'd like to programatically select a row in my NSTableView, with just one column.

func selectColumnIndexes(_ indexes: NSIndexSet!,
    byExtendingSelection extend: Bool)

I have been playing around with this, but I am not sure how to write "Select row #2".
Do I have to start with the variable connected to the @IBOutlet of my Table View? I have to indicate it is about my NSTableView somehow.

Thanks!


回答1:


Swift 4 Solution

I created an extension to make a little easier to do this. Also if you implemented a click action on the table view cell this will invoke it as well

extension NSTableView {
    func selectRow(at index: Int) {
        selectRowIndexes(.init(integer: index), byExtendingSelection: false)
        if let action = action {
            perform(action)
        }
    }
}

/// USAGE:
NSTableView().selectRow(at: 0)



回答2:


@Isaiah answer in Swift 3.0:

.selectRowIndexes(NSIndexSet(index: 0) as IndexSet, byExtendingSelection: false)



回答3:


Okay, I've got it. It turned out to be

@IBOutlet weak var NoteTableView: NSTableView!
NoteTableView.selectRowIndexes(NSIndexSet(index: 0), byExtendingSelection: false)

I couldn't quite figure out the part with NSIndexSet. I was fiddling around with init(), which turned out to be unnecessary.



来源:https://stackoverflow.com/questions/25272663/swift-nstableview-simply-select-a-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!