NSTableView 获取具有单元格的 indexPath
NSTableView get indexPath having the cell
使用 UITableView
可以很容易地找到基于使用
的单元格的 indexPath
tableView.indexPath(for: cell)
但遗憾的是在 MacOS 上不存在此方法。
我的用例如下:
我在 Cell View
中有一个按钮,单击后我会调用委派的 ViewController
来执行操作。现在我有了单元格的实例,我要获取的是它在NSTableView
中的行(位置)。如何获得?
在 macOS 中使用基于视图的 table 视图,为按钮创建标准 IBAction
并获取包含 row(for view:
的行
func row(for view: NSView) -> Int
Returns the index of the row for the specified view.
基于视图的 table 视图更加通用且更易于处理。
要在@vadian 的回答中添加具体案例:
我遇到了与@rdid4 相同的情况,但有多个 tableView。我输入了这个:
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
switch (tableView){
let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("projectCell1"), owner: self) as? ProjectCell
if (tableColumn?.identifier)!.rawValue == "projectColumn1" {
let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("projectCell1"), owner: self) as? ProjectCell
cellView 有一个内联 NSButton
cellView?.statusButton.action = #selector(projectStatusChanged(sender:))
cellView?.statusButton.tag = 1
单元格标签向我展示了我需要处理的 NSTableView(我在项目中有两个)。
然后在
@objc func projectStatusChanged(sender: NSButton) {
switch (sender.tag){
case 1:
projectRowIndex = projectTable1.row(for: sender)
....
case 2:
projectRowIndex = projectTable2.row(for: sender)
default...
我希望这对其他人有帮助..
使用 UITableView
可以很容易地找到基于使用
indexPath
tableView.indexPath(for: cell)
但遗憾的是在 MacOS 上不存在此方法。
我的用例如下:
我在 Cell View
中有一个按钮,单击后我会调用委派的 ViewController
来执行操作。现在我有了单元格的实例,我要获取的是它在NSTableView
中的行(位置)。如何获得?
在 macOS 中使用基于视图的 table 视图,为按钮创建标准 IBAction
并获取包含 row(for view:
func row(for view: NSView) -> Int
Returns the index of the row for the specified view.
基于视图的 table 视图更加通用且更易于处理。
要在@vadian 的回答中添加具体案例:
我遇到了与@rdid4 相同的情况,但有多个 tableView。我输入了这个:
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
switch (tableView){
let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("projectCell1"), owner: self) as? ProjectCell
if (tableColumn?.identifier)!.rawValue == "projectColumn1" {
let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("projectCell1"), owner: self) as? ProjectCell
cellView 有一个内联 NSButton
cellView?.statusButton.action = #selector(projectStatusChanged(sender:))
cellView?.statusButton.tag = 1
单元格标签向我展示了我需要处理的 NSTableView(我在项目中有两个)。
然后在
@objc func projectStatusChanged(sender: NSButton) {
switch (sender.tag){
case 1:
projectRowIndex = projectTable1.row(for: sender)
....
case 2:
projectRowIndex = projectTable2.row(for: sender)
default...
我希望这对其他人有帮助..