xcode Swift & NSTableView - 只需选择一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25272663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:22:40  来源:igfitidea点击:

Swift & NSTableView - Simply select a row

xcodeselectswiftrownstableview

提问by Isaiah

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

我想以编程方式在我的 NSTableView 中选择一行,只有一列。

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 @IBOutletof my Table View? I have to indicate it is about my NSTableView somehow.

我一直在玩这个,但我不知道如何写“选择第 2 行”。
我是否必须从连接到@IBOutlet我的表视图的变量开始?我必须以某种方式表明它是关于我的 NSTableView 的。

Thanks!

谢谢!

采纳答案by Charlton Provatas

Swift 4 Solution

Swift 4 解决方案

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)

回答by curt

@Isaiah answer in Swift 3.0:

@Isaiah 在 Swift 3.0 中的回答:

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

回答by Isaiah

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.

我无法完全弄清楚NSIndexSet. 我正在摆弄init(),结果证明这是不必要的。