Mr. X
Mr. X

Reputation: 3

Can not insert items into NSOutlineView

I've been fighting with this NSOutlineView that should insert new items to the end of a group. 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.

    @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)

    }
}

Upvotes: 0

Views: 635

Answers (2)

quan
quan

Reputation: 89

Because you are missing this sentence

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

Upvotes: 0

A.C. Wright
A.C. Wright

Reputation: 941

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.

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.

Upvotes: 1

Related Questions