A bit of background first:
I have a document based app that's a file viewer. Each file contains a bunch of tables. Each document window is a horizontal NSStackView. The left hand view shows a list of tables in the file and the right hand view is an NSTabView with two tabs. One to show the unformatted version of the table (i.e. a hex dump) and the other to show a formatted view. Currently the formatted view is a scrollable NSTableView with a single table cell.
And my (probably stupid) questions:
I want to use a different view for each table type, but I couldn't figure out a way to do that with AppKit. When I learned about SwiftUI, I though it would be easy to create a view that changed based on the table type, and I could put this view into an NSHostingView. I have two questions:
1) I thought there'd be a way to create an NSHostingView in the storyboard, just like there is for an NSHostingViewController, but I don't see any way to to that. Have I missed somthing? Is this an as yet unimplemented feature, or is there another way to do it.
2) I tried to make a SwiftUIView that contains a different view depending on the type of the selected table, but I can't figure out how to do this. Here's what I tried:
import SwiftUI
@available(OSX 10.15.0, *)
struct FormattedView: View {
var table: Table
var body: some View {
formattedView(table.type)
}
func formattedView(_ tableType: TableType) -> some View {
List {
if tableType == TableType.oneType {
OneTypeTableFormattedView(table)
} else if tableType == TableType.anotherType {
AnotherTypeTableFormattedView(table)
} else {
Text("No formatted view for \(tableType) table")
}
}
}
}
The formattedView function doesn't compile unless I embed the body in a List. It also dosen't compile if I use a switch statement instead of a if-else chain. I guess this means I'm not on the right track here. Is there a better way to do this?