【问题标题】:Can not insert items into NSOutlineView无法将项目插入 NSOutlineView
【发布时间】:2017-10-11 04:00:59
【问题描述】:

我一直在使用应该在组末尾插入新项目的 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)

    }
}

【问题讨论】:

  • 可能不相关,但你为什么要使用那个麻烦的unowned self?尽管许多人断言 GCD 确实不会导致保留周期。
  • 重新加载大纲视图。
  • @ElTomato 不,不要,insertItems 应该更新 UI。
  • 谢谢,使用reloadData 确实会显示所有项目,但它也会关闭所有展开的行,这是我试图避免的。
  • insertItems 没有重新加载我的大纲视图。不知道为什么。

标签: swift macos cocoa


【解决方案1】:

我怀疑你遇到的麻烦是因为这条线:

unownedSelf.outlineview.insertItems(at: IndexSet([index]),
                                                inParent: unownedSelf.sidebarDataSource?.allFeeds,
                                                withAnimation: .slideRight)

我假设allFeeds 是一个数据对象数组,而inParent 参数需要大纲视图树中的一个项目(行),或者nil 表示根项目。

您可以使用this 方法获取对大纲视图项的引用:

func item(atRow row: Int) -> Any?

您使用此行将新数据添加到数据源是正确的:

unownedSelf.sidebarDataSource?.allFeeds.append(newFeed)

因此,当您重新加载应用程序时,您可能会在大纲视图的某处调用 reloadData 并看到新数据出现。

【讨论】:

  • 谢谢,我的数据源稍微复杂一些(只是一个包含多个数组的结构),但这为我指明了正确的方向。我应该一直在尝试从大纲视图而不是数据源中检索父对象。
【解决方案2】:

因为你漏掉了这句话

unownedSelf.outlineview.expandItem(unownedSelf.sidebarDataSource?.allFeeds, expandChildren: true)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    相关资源
    最近更新 更多