Created
March 26, 2022 12:12
-
-
Save camyoh/568768c02df96fc781c740c553403d6a to your computer and use it in GitHub Desktop.
Simple Table View with data
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
// | |
// ViewController.swift | |
// prefetching | |
// | |
// Created by Andres Mendieta on 3/26/22. | |
// | |
import UIKit | |
/// Primary app view controller | |
final class ViewController: UIViewController { | |
/// TableView Component | |
private let tableView: UITableView = { | |
let tableView = UITableView() | |
tableView.backgroundColor = .systemBackground | |
tableView.translatesAutoresizingMaskIntoConstraints = false | |
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") | |
tableView.rowHeight = UITableView.automaticDimension | |
tableView.estimatedRowHeight = 300 | |
return tableView | |
}() | |
/// ViewModels | |
private let viewModels = Array(1...100).map { "Hello world: \($0)" } | |
// MARK: - Lifecycle | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.addSubview(tableView) | |
addConstraints() | |
setUpTable() | |
} | |
/// Sets up table | |
private func setUpTable() { | |
tableView.delegate = self | |
tableView.dataSource = self | |
} | |
private func addConstraints(){ | |
NSLayoutConstraint.activate([ | |
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor), | |
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor), | |
tableView.topAnchor.constraint(equalTo: view.topAnchor), | |
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor), | |
]) | |
} | |
} | |
extension ViewController: UITableViewDataSource, UITableViewDelegate { | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return viewModels.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) | |
var content = cell.defaultContentConfiguration() | |
content.text = viewModels[indexPath.row] | |
cell.contentConfiguration = content | |
return cell | |
} | |
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | |
return UITableView.automaticDimension | |
} | |
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { | |
return 300 | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment