let kscreenwidth = UIScreen.main.bounds.size.width
//屏幕的高
let kscreenheight = UIScreen.main.bounds.size.height
class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource {
var dataArray:[String] = ["张三","李四","刘静","王二","峰少","文达"]
var tableview:UITableView!
let identifier = "cell"
override func loadView() {
tableview = UITableView(frame: UIScreen.main.bounds, style: .plain)
//数据源代理
tableview.dataSource = self
//业务代理属性
tableview.delegate = self
self.view = tableview
}
override func viewDidLoad() {
super.viewDidLoad()
tableview.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: identifier)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: identifier)
cell?.textLabel?.text = "通讯录"
cell?.backgroundColor = #colorLiteral(red: 1, green: 0.8886405958, blue: 0.8651009388, alpha: 1)
return cell!
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return (indexPath.section % 2 == 0) ? 40: 80
}
//返回分区头部的高度
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 100
}
//返回分区尾部的高度
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if section == 0 {
return 50
}
return 100
}
//返回分区区头视图的方法
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionheaderview = UIView(frame: CGRect(x: 0, y: 0, width: kscreenwidth, height: 100))
sectionheaderview.backgroundColor = #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)
return sectionheaderview
}
//返回区尾视图的方法
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let sectionfooterview = UIView(frame: CGRect(x: 0, y: 0, width: kscreenwidth, height: 50))
sectionfooterview.backgroundColor = #colorLiteral(red: 0.1215686277, green: 0.01176470611, blue: 0.4235294163, alpha: 1)
return sectionfooterview
}
//重点中的重点 选中cell会触发的方法
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let detailvc = DetailViewController()
detailvc.indexpath = indexPath
self.navigationController?.pushViewController(detailvc, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}