xcode 在 Cocoa OS X 的 NSTableView 中选择单元格时调用哪个方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10796724/
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
which method is called when selecting a cell in the NSTableView in Cocoa OS X?
提问by Gaurav_soni
I have a NSTableView , i want to get the value present in the cell. I am having only one column so , i just need the row number
我有一个 NSTableView ,我想获取单元格中存在的值。我只有一列,所以我只需要行号
i can use this [tableView selectedRow]- but where do i put this i want to put this in a method that gets called on selection of any of the rows.
我可以使用这个 [tableView selectedRow]- 但是我把它放在哪里我想把它放在一个方法中,该方法在选择任何行时被调用。
-(void)tableViewSelectionDidChange:(NSNotification *)notification{
NSLog(@"%d",[tableViewController selectedRow]);
}
The above method also does not work i am getting the error -[NSScrollView selectedRow]: unrecognized selector sent to instance 0x100438ef0]
上述方法也不起作用我收到错误 -[NSScrollView selectedRow]: unrecognized selector sent to instance 0x100438ef0]
i want something like the method available in the iPhone tableview-
我想要类似 iPhone 表格视图中可用的方法的东西-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
回答by Eimantas
What is tableViewController
object? Only NSTableView
instances respond to selectedRow
. You can get current table view (the one that sent the notification) from notification
's object property:
什么是tableViewController
对象?只有NSTableView
实例响应selectedRow
. 您可以从notification
的对象属性获取当前表视图(发送通知的表视图):
Objective-C:
目标-C:
-(void)tableViewSelectionDidChange:(NSNotification *)notification{
NSLog(@"%d",[[notification object] selectedRow]);
}
Swift:
迅速:
func tableViewSelectionDidChange(notification: NSNotification) {
let table = notification.object as! NSTableView
print(table.selectedRow);
}
回答by ingconti
my 2 cents for Xcode 10/swift 4.2
我的 2 美分 Xcode 10/swift 4.2
func tableViewSelectionDidChange(_ notification: Notification) {
guard let table = notification.object as? NSTableView else {
return
}
let row = table.selectedRow
print(row)
}
回答by styl3r
Swift 3 (from Eimantas' answer):
Swift 3(来自 Eimantas 的回答):
func tableViewSelectionDidChange(_ notification: NSNotification) {
let table = notification.object as! NSTableView
print(table.selectedRow);
}
回答by guo.yi
You should addObserver Notification like this
你应该像这样添加Observer Notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(tableViewSelectionDidChangeNotification)
name:NSTableViewSelectionDidChangeNotification object:nil];
It will action when tableView selected row
当 tableView 选择行时它会动作
good luck
祝你好运
回答by black_pearl
full guide In Swift 5:
Swift 5 中的完整指南:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.didSelectRow(_:)), name: NSTableView.selectionDidChangeNotification, object: tableView)
}
@objc
func didSelectRow(_ noti: Notification){
guard let table = noti.object as? NSTableView else {
return
}
let row = table.selectedRow
print(row)
}
deinit {
NotificationCenter.default.removeObserver(self)
}