Apple offers the following SwiftUI List
initializer:
List<SelectionValue: Hashable, Content: View>.init(selection: Binding<SelectionValue?>?, content: () -> Content)
I would like to make a similar component, but customizing some of the UITableView
behaviors not exposed in List
. Also, I would like to mirror this single-selection, data-less initializer, because I don't have much arbitrary data, and would like to define my rows in-line in a ViewBuilder closure. So, wrapping UITableView
:
struct CustomizedList<SelectionValue: Hashable, Content: View>: UIViewRepresentable {
@Binding var selection: SelectionValue
@ViewBuilder var content: () -> Content
func makeUIView(context: Context) -> UITableView {
let tableView = UITableView()
tableView.delegate = context.coordinator
tableView.dataSource = content.coordinator
return tableView
}
func updateUIView(_ tableView: UITableView, context: Context) {
// todo
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
final class Coordinator: NSObject, UITableViewDelegate, UITableViewDataSource {
private var parent: CustomizedList
init(_ parent: CustomizedList) {
self.parent = parent
}
// delegate methods to customize behavior
// ...
// data source methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// ???
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// ???
}
}
}
What do I do in the data source delegate methods? Since List
is a wrapper around UITableView
, how is Apple implementing the data source delegate methods for the data-less initializers of List
? (Also notice that for List
's data initializers, Data
is only a generic of the initializer, not List
itself.)
Is it possible to use UITableView
without a data source, and just pass on the Content
(with some View
-to-UIView
wrapping)?