Created
June 17, 2020 03:18
-
-
Save fromkk/fde679d0f9e06fe509cfde9b0f350b4e to your computer and use it in GitHub Desktop.
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 | |
class HeaderView: UIView { | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setUp() | |
} | |
required init?(coder: NSCoder) { | |
super.init(coder: coder) | |
setUp() | |
} | |
private lazy var setUp: () -> Void = { | |
label.translatesAutoresizingMaskIntoConstraints = false | |
addSubview(label) | |
NSLayoutConstraint.activate([ | |
label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16), | |
label.topAnchor.constraint(equalTo: topAnchor, constant: 16), | |
trailingAnchor.constraint(equalTo: label.trailingAnchor, constant: 16), | |
bottomAnchor.constraint(equalTo: label.bottomAnchor, constant: 16) | |
]) | |
return {} | |
}() | |
lazy var label: UILabel = { | |
let label = UILabel() | |
label.numberOfLines = 0 | |
label.lineBreakMode = .byWordWrapping | |
label.text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." | |
return label | |
}() | |
} | |
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(tableView) | |
NSLayoutConstraint.activate([ | |
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor), | |
view.trailingAnchor.constraint(equalTo: tableView.trailingAnchor), | |
tableView.topAnchor.constraint(equalTo: view.topAnchor), | |
view.bottomAnchor.constraint(equalTo: tableView.bottomAnchor) | |
]) | |
layoutHeaderView() | |
} | |
override func viewDidLayoutSubviews() { | |
super.viewDidLayoutSubviews() | |
headerView.layoutIfNeeded() | |
} | |
lazy var headerView = HeaderView() | |
lazy var tableView: UITableView = { | |
let tableView = UITableView(frame: view.bounds, style: .plain) | |
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") | |
tableView.dataSource = self | |
tableView.delegate = self | |
return tableView | |
}() | |
private func layoutHeaderView() { | |
headerView.translatesAutoresizingMaskIntoConstraints = false | |
tableView.tableHeaderView = headerView | |
NSLayoutConstraint.activate([ | |
headerView.topAnchor.constraint(equalTo: tableView.topAnchor), | |
headerView.widthAnchor.constraint(equalTo: tableView.widthAnchor), | |
headerView.centerXAnchor.constraint(equalTo: tableView.centerXAnchor)]) | |
headerView.layoutIfNeeded() | |
tableView.tableHeaderView = headerView | |
} | |
func numberOfSections(in tableView: UITableView) -> Int { | |
return 1 | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return 10 | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) | |
cell.textLabel?.text = "Number \(indexPath.row)" | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment