NSOutlineView 用 NSViewRepresentable 包装时不会调用委托

问题描述 投票:0回答:1

由于

DisclosureGroup
功能与
List
的限制,我正在考虑使用
NSOutlineView
包裹着
NSViewRepresentable

但是,通过这个简单的示例,当我创建视图时,我没有看到在协调器上调用所有必需的

NSOutlineViewDataSource
函数,我认为这些函数应该填充视图。仅调用
outlineView(_:numberOfChildrenOfItem:)
,然后调用
updateNSView(_:context:)

我添加了一个边框来检查包装视图实际上是在

ContentView
中绘制的,看起来确实如此。

我错过了什么?

包装 NSOutlineView:

import SwiftUI

struct AppKitOutlineView: NSViewRepresentable {
    
    //ViewRepresentable Stuff
    
    class Coorindinator: NSObject, NSOutlineViewDataSource{
        
        var theData = ["One", "Two", "Three", "Four"]

        //NSOutlineViewDataSource Stuff
        
        func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
            print(#function)
            return theData[index]
        }
        
        func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
            print(#function)
            return false
        }
        
        func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
            print(#function)
            print (theData.count)
            return theData.count
        }
        
        func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? {
            print(#function)
            return item    
        }
        
    }
    
    func makeCoordinator() -> Coorindinator {
        print(#function)
        return Coorindinator()
    }
    
    func makeNSView(context: Context) -> some NSView {
        print(#function)
        let outlineView = NSOutlineView()
        outlineView.dataSource = context.coordinator
        return outlineView
    }
    
    func updateNSView(_ nsView: NSViewType, context: Context) {
        print(#function)
        //Nothing yet
    }
} 

内容视图:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack{
            Text("Thing")
            AppKitOutlineView().border(Color.red)
        }
    }
}

这是我在控制台中看到的情况:

makeCoordinator()
makeNSView(context:)
outlineView(_:numberOfChildrenOfItem:)
4
updateNSView(_:context:)

这是我在程序用户界面中看到的:

swiftui appkit nsoutlineview nsviewrepresentable
1个回答
0
投票

其他数据源取决于大纲视图外观,在您的示例中返回 nil。所以这就是为什么它没有被调用。尝试使

viewFor tableColumn
符合
NSOutlineViewDelegate

func makeNSView(context: Context) -> some NSView {
    ...
    outlineView.delegate = context.coordinator
}

class Coorindinator: NSObject, NSOutlineViewDataSource, NSOutlineViewDelegate {
    func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
        let view = NSTableCellView()
        //More configurations here
        return view
    }
    ...
}

那么输出应该是:

makeCoordinator()
makeNSView(context:)
outlineView(_:numberOfChildrenOfItem:)
4
outlineView(_:child:ofItem:)
outlineView(_:isItemExpandable:)
outlineView(_:child:ofItem:)
outlineView(_:isItemExpandable:)
outlineView(_:child:ofItem:)
outlineView(_:isItemExpandable:)
outlineView(_:child:ofItem:)
outlineView(_:isItemExpandable:)
updateNSView(_:context:)
updateNSView(_:context:)
makeCoordinator()
makeNSView(context:)
outlineView(_:numberOfChildrenOfItem:)
...
© www.soinside.com 2019 - 2024. All rights reserved.