Created
October 16, 2021 14:41
-
-
Save agiokas/0ea6994cfed0d357a77885253aa8445b to your computer and use it in GitHub Desktop.
UIKit - SwiftUI Comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
@ObservedObject var viewModel: ContentVM | |
var body: some View { | |
NavigationView { | |
List(viewModel.values, id: \.self) { value in | |
Text(value) | |
}.navigationBarTitle("Our Cells") | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Combine | |
class UNavContentVC: UINavigationController { | |
init(viewModel: ContentVM) { | |
super.init(rootViewController: UContentVC(viewModel: viewModel)) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
class UContentVC: UIViewController, UITableViewDataSource { | |
private var cancelables = [AnyCancellable]() | |
let tableView = UITableView() | |
let viewModel: ContentVM | |
init(viewModel: ContentVM) { | |
self.viewModel = viewModel | |
super.init(nibName: nil, bundle: nil) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.title = "Our Cells" | |
self.navigationController?.navigationBar.prefersLargeTitles = true | |
self.tableView.frame = self.view.bounds | |
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "someCell") | |
self.tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight] | |
self.view.addSubview(self.tableView) | |
self.tableView.dataSource = self | |
viewModel.$values.sink { [weak self] _ in | |
self?.tableView.reloadData() | |
}.store(in: &cancelables) | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
viewModel.values.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "someCell", for: indexPath) | |
cell.textLabel?.text = viewModel.values[indexPath.row] | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment