UITableView 相关
2020-02-28 本文已影响0人
AntKing
tableHeaderView 和 tableFooterView
- UITableView属性里的tableHeaderView和tableFooterView是和UITableView一起滚动的,delegate方法里的是不会跟随一起滚动的
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@objc dynamic private lazy var tableView: UITableView = {
let view = UITableView(frame: CGRect.zero, style: UITableView.Style.plain)
view.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.frame = self.view.bounds
let tableHaderView = UIView()
tableHaderView.frame = CGRect.init(x: 0, y: 0, width: 414, height: 50)
tableHaderView.backgroundColor = UIColor.yellow
self.tableView.tableHeaderView = tableHaderView
let tableFooterView = UIView()
tableFooterView.frame = CGRect.init(x: 0, y: 0, width: 414, height: 30)
tableFooterView.backgroundColor = UIColor.black
self.tableView.tableFooterView = tableFooterView
self.view.addSubview(self.tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 40
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = "\(indexPath.row)"
return cell!
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView()
header.frame = CGRect.init(x: 0, y: 0, width: 414, height: 50)
header.backgroundColor = UIColor.red
return header
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footer = UIView()
footer.frame = CGRect.init(x: 0, y: 0, width: 414, height: 50)
footer.backgroundColor = UIColor.green
return footer
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 100
}
}