简体   繁体   English

无法将项目插入 NSOutlineView

[英]Can not insert items into NSOutlineView

I've been fighting with this NSOutlineView that should insert new items to the end of a group.我一直在与这个 NSOutlineView 作斗争,它应该将新项目插入到组的末尾。 My code is being called/hitting breakpoints but the new items don't appear in the NSOutlineView until I kill and restart the app.Not sure what I'm missing.我的代码正在被调用/命中断点,但新项目不会出现在 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)

    }
}

I suspect the trouble you are having is because of this line: 我怀疑您遇到的麻烦是由于以下原因:

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

I assume allFeeds is an array of data objects, and inParent argument expects an item (row) in the outline view's tree, or nil which would mean the root item. 我假设allFeeds是一个数据对象数组,并且inParent参数期望大纲视图树中有一个项目(行),或者nil表示根项目。

You can get a reference to an outline view item with this method: 您可以使用以下方法获得对大纲视图项目的引用:

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

You are correct in adding the new data to your data source with this line: 您可以使用此行将新数据添加到数据源中,这是正确的:

unownedSelf.sidebarDataSource?.allFeeds.append(newFeed)

So when you reload the application you are likely calling reloadData somewhere on the outline view and seeing the new data appear. 因此,当您重新加载应用程序时,您可能会在大纲视图的某处调用reloadData并看到出现了新数据。

Because you are missing this sentence因为你错过了这句话

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM