swift - 如何获取 NSTableView 中选定的行?查看(位于列 :row:makeIfNecessary:) return nil

标签 swift macos nstableview

我有一个 NSTableview,其内容绑定(bind)到 NSArrayController。 并且其 selectionIndexes 绑定(bind)到 arrayController 的 selectionIndexes。 并且此 arrayController 绑定(bind)到 NSManagedObjectContext

因为 arrayController 的 selectsInsertedObjects 为 true,所以当我向 NSManagedObjectContext 添加对象时,tableViewSelectionDidChange(_ notification: notification) 将被调用。我在这里设置了一个断点并在控制台中进行调试(我将 tableview 的委托(delegate)设置为其自身):

(lldb) po selectedRow
11
(lldb) po selectedCell()
nil
(lldb) po numberOfColumns
2
(lldb) po numberOfRows
12
(lldb) po view(atColumn: 0, row: 0, makeIfNecessary: false)
nil
(lldb) po view(atColumn: 0, row: selectedRow, makeIfNecessary: false)
nil

我可以在表格 View 中看到几行。

但是为什么po view(atColumn: 0, row: 0, makeIfNecessary: false)返回nil?

如何在委托(delegate)方法中获取选定的行tableViewSelectionDidChange(_ notification: notification)

最佳答案

我怀疑 TableView 在更新其布局之前发布了NSTableViewSelectionDidChangeNotification。因此,您在 TableView 创建 subview 之前向其请求 subview 。

这就是 makeIfNecessary 参数的用途。试试这个方法:

po view(atColumn: 0, row: selectedRow, makeIfNecessary: true)

关于swift - 如何获取 NSTableView 中选定的行?查看(位于列 :row:makeIfNecessary:) return nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44994719/

相关文章:

macos - 如何在图层主机 NSView 中使用 NSVisualEffectView

objective-c - 使用 NSUserDefaults 保存从 NSPopupbutton 中选择的项目

python - 使用 anaconda python 3.52 导入 sklearn 错误

XCode Developer API - 对象库 - 对象消失

swift - 类型 "URL"没有类型 "fileURL"- 无法将字符串数组作为字符串传递

swift - NSCollectionView 在同一点呈现所有 NSCollectionViewItems

swift - 无法设置委托(delegate)来重新加载 TableView

cocoa - 如何在没有动画的情况下滚动RowToVisible

ios - 自定义单元格的刷新控制问题 - Swift 4 IOS 11

ios - Swift - 将渐变设置为 UITableView 部分背景