Last active
February 16, 2020 15:54
-
-
Save yzhong52/ad7306718fe09aa697a342eee657c836 to your computer and use it in GitHub Desktop.
Building a Client App From Scratch (ViewController with Client and TableView)
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 RxSwift | |
import SafariServices | |
class ViewController: UIViewController { | |
private let disposeBag = DisposeBag() | |
private let client = NewsClient() | |
private var articles: [Article] = [] | |
private let tableView: UITableView = { | |
let tableView = UITableView() | |
tableView.translatesAutoresizingMaskIntoConstraints = false | |
tableView.tableFooterView = UIView() | |
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") | |
tableView.rowHeight = UITableView.automaticDimension | |
tableView.estimatedRowHeight = 134 | |
return tableView | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.addSubview(tableView) | |
NSLayoutConstraint.activate([ | |
tableView.topAnchor.constraint(equalTo: view.topAnchor), | |
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor), | |
tableView.leftAnchor.constraint(equalTo: view.leftAnchor), | |
tableView.rightAnchor.constraint(equalTo: view.rightAnchor), | |
]) | |
tableView.delegate = self | |
tableView.dataSource = self | |
client.headlines().subscribe(onSuccess: { [weak self] (response) in | |
self?.articles = response.articles | |
self?.tableView.reloadData() | |
}, onError: { error in | |
print("Received error \(error)") | |
}).disposed(by: disposeBag) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment