Skip to content

Instantly share code, notes, and snippets.

@Quaggie
Created March 14, 2018 17:22
Show Gist options
  • Save Quaggie/7900d526f79a86f80e9e42341dcb5981 to your computer and use it in GitHub Desktop.
Save Quaggie/7900d526f79a86f80e9e42341dcb5981 to your computer and use it in GitHub Desktop.
class ViewController: UIViewController {
private let motorcycles: [Motorcycle]
private lazy var tableview: UITableView = {
let tableview = UITableView()
tableview.translatesAutoresizingMaskIntoConstraints = false
tableview.dataSource = self
tableview.contentInset = UIEdgeInsets(top: 16, left: 0, bottom: 16, right: 0)
return tableview
}()
init() {
motorcycles = [
Motorcycle(image: #imageLiteral(resourceName: "hayabusa"), name: "Suzuki Hayabusa", type: .sport),
Motorcycle(image: #imageLiteral(resourceName: "tiger"), name: "Triumph Tiger 1200 XC", type: .bigTrail),
Motorcycle(image: #imageLiteral(resourceName: "streetbob"), name: "Harley Davidson Street Bob", type: .custom)
]
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Motorcycles"
view.addSubview(tableview)
tableview.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
tableview.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
tableview.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
tableview.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return motorcycles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell.textLabel?.text = motorcycle.name
cell.imageView?.image = motorcycle.image
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment