无法将项目插入NSOutlineView

时间:2017-10-11 04:01:00

标签: swift macos cocoa

我一直在与这个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)

    }
}

1 个答案:

答案 0 :(得分:0)

我怀疑你遇到的麻烦是因为这句话:

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并看到新数据。