Обтекание UITableView в SwiftUI - PullRequest
0 голосов
/ 11 октября 2019

У меня проблема с переносом UITableView в Swift UI.

Я использую Coordinator для переноса, но у меня возникают проблемы с динамическим обновлением источника данных.

struct UIKitTableView: UIViewRepresentable {

@Binding var data: [String]

func makeUIView(context: Context) -> UITableView {

    let tableView = UITableView(frame: .zero)
    tableView.backgroundColor = .clear
    tableView.translatesAutoresizingMaskIntoConstraints = false
    tableView.dataSource = context.coordinator
    tableView.delegate = context.coordinator
    TableViewCell.registerWithTableView(tableView: tableView)
    return tableView

}

func updateUIView(_ uiView: UITableView, context: Context) {
    print ("ricarico con \(data.count) valori")
    makeCoordinator()
    uiView.dataSource = context.coordinator
    uiView.delegate = context.coordinator
    uiView.reloadData()

}

func makeCoordinator() -> Coordinator {
    Coordinator(self)
}

А вот моя реализация координатора

class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate {

    var parent: UIKitTableView

    init(_ tableView: UIKitTableView) {
        self.parent = tableView
    }

    // MARK: UITableViewViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(parent.data.count)
        return self.parent.data.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let myCell = TableViewCell.getReusedCellFrom(tableView: tableView, cellForItemAt: indexPath)
        myCell.backgroundColor = .clear
        myCell.cellLabel.text = "Riga \(indexPath.row) \(self.parent.data[indexPath.row])"
        myCell.applyConstraints2(myLabel: myCell.cellLabel, myCell: myCell)
        return myCell
    }
}

Проблема в том, что журналы в UIKitTableView сообщают мне, что данные изменились, но в реализации Координатора TableViewDelegates это не изменится. Если вам нужно больше фрагментов контекста, скажите мне.

...