Skip to content

Instantly share code, notes, and snippets.

@yzhong52
Last active February 16, 2020 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yzhong52/ad7306718fe09aa697a342eee657c836 to your computer and use it in GitHub Desktop.
Save yzhong52/ad7306718fe09aa697a342eee657c836 to your computer and use it in GitHub Desktop.
Building a Client App From Scratch (ViewController with Client and TableView)
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