我一直在与这个应该将新项目插入到组末尾的 NSOutlineView 作斗争。我的代码正在被调用/遇到断点,但新项目不会出现在 NSOutlineView 中,直到我终止并重新启动应用程序。不确定我遗漏了什么。
@objc func didReceaveNewFeeds(aNotification: Notification) {
guard let userinfo: [AnyHashable : Any] = aNotification.userInfo,
let newFeed: ManagedFeed = userinfo[Notification.Name.newFeedKey] as? ManagedFeed else {
return
}
let index: Int = sidebarDataSource?.allFeeds.count ?? 0
unowned let unownedSelf: MainViewController = self
DispatchQueue.main.async {
unownedSelf.sidebarDataSource?.allFeeds.append(newFeed)
unownedSelf.outlineview.insertItems(at: IndexSet([index]),
inParent: unownedSelf.sidebarDataSource?.allFeeds,
withAnimation: .slideRight)
}
}
最佳答案
我怀疑你遇到的麻烦是因为这一行:
unownedSelf.outlineview.insertItems(at: IndexSet([index]),
inParent: unownedSelf.sidebarDataSource?.allFeeds,
withAnimation: .slideRight)
我假设 allFeeds
是一个数据对象数组,inParent
参数需要大纲 View 树中的一个项目(行),或者 nil
这意味着根项。
您可以使用 this 获取对大纲 View 项的引用方法:
func item(atRow row: Int) -> Any?
您使用此行将新数据添加到数据源是正确的:
unownedSelf.sidebarDataSource?.allFeeds.append(newFeed)
因此,当您重新加载应用程序时,您可能会在大纲 View 的某处调用 reloadData
并看到新数据出现。
关于swift - 无法将项目插入 NSOutlineView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46679590/