如何在不使用 NSTreeController 的情况下获取 NSOutlineView 的选定项?

发布于 2024-08-21 12:08:04 字数 231 浏览 6 评论 0原文

如何使用我自己的数据源获取 NSOutlineView 的选定项目。 我看到我可以获得 selectedRow 但它返回一个相对于大纲状态的行 ID。唯一的方法是跟踪项目的展开折叠状态,但这似乎很荒谬。

我希望得到类似的结果:

array = [outlineViewOutlet selectedItems];

我查看了其他类似的问题,他们似乎没有回答这个问题。

How do I get the selected item of an NSOutlineView with using my own data source.
I see I can get selectedRow but it returns a row ID relative to the state of the outline. The only way to do it is to track the expanded collapsed state of the items, but that seems ridiculous.

I was hoping for something like:

array = [outlineViewOutlet selectedItems];

I looked at the other similar questions, they dont seem to answer the question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福不弃 2024-08-28 12:08:04

NSOutlineView 继承自 NSTableView,因此您可以获得很好的方法,例如 selectedRow

id selectedItem = [outlineView itemAtRow:[outlineView selectedRow]];

NSOutlineView inherits from NSTableView, so you get nice methods such as selectedRow:

id selectedItem = [outlineView itemAtRow:[outlineView selectedRow]];
少女净妖师 2024-08-28 12:08:04

Swift 5

NSOutlineView 有一个委托方法outlineViewSelectionDidChange

 func outlineViewSelectionDidChange(_ notification: Notification) {

    // Get the outline view from notification object
    guard let outlineView = notification.object as? NSOutlineView else {return}

    // Here you can get your selected item using selectedRow
    if let item = outlineView.item(atRow: outlineView.selectedRow) {
      
    }
}

额外提示:您还可以获取所选项目的父项目像这样:

func outlineViewSelectionDidChange(_ notification: Notification) {

// Get the outline view from notification object
guard let outlineView = notification.object as? NSOutlineView else {return}

// Here you can get your selected item using selectedRow
if let item = outlineView.item(atRow: outlineView.selectedRow) {
  
     // Get the parent item
      if let parentItem = outlineView.parent(forItem: item){
            
      }  
   } 
}

Swift 5

NSOutlineView has a delegate method outlineViewSelectionDidChange

 func outlineViewSelectionDidChange(_ notification: Notification) {

    // Get the outline view from notification object
    guard let outlineView = notification.object as? NSOutlineView else {return}

    // Here you can get your selected item using selectedRow
    if let item = outlineView.item(atRow: outlineView.selectedRow) {
      
    }
}

Bonus Tip: You can also get the parent item of the selected item like this:

func outlineViewSelectionDidChange(_ notification: Notification) {

// Get the outline view from notification object
guard let outlineView = notification.object as? NSOutlineView else {return}

// Here you can get your selected item using selectedRow
if let item = outlineView.item(atRow: outlineView.selectedRow) {
  
     // Get the parent item
      if let parentItem = outlineView.parent(forItem: item){
            
      }  
   } 
}
风苍溪 2024-08-28 12:08:04

@Dave De Long:很好的答案,这里是 Swift 3.0 的翻译。

@objc private func onItemClicked() {
    if let item = outlineView.item(atRow: outlineView.clickedRow) as? FileSystemItem {
        print("selected item url: \(item.fileURL)")
    }
}

显示的是 item 来自具有 fileURL 属性的类 FileSystemItem 的情况。

@Dave De Long: excellent answer, here is the translation to Swift 3.0

@objc private func onItemClicked() {
    if let item = outlineView.item(atRow: outlineView.clickedRow) as? FileSystemItem {
        print("selected item url: \(item.fileURL)")
    }
}

Shown is a case where item is from class FileSystemItem with a property fileURL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文