UITableView用于将数据以列表的形式展示出来,以及一些其它的页面布局,在开发中经常会被用到!
1.创建UITableView
class TableViewController:UITableViewDelegate,UITableViewDataSource{
//初始化表格
func initTableView(){
let rect = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
let table = UITableView(frame: rect)
self.view.addSubview(table)
//设置数据源和代理
table.delegate = self
table.dataSource = self
}
}
2.必须实现的代理方法
//每组行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
//行高
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
//设置每行Cell样式
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell.init(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 80))
let label = UILabel(frame:CGRect(x: 30, y: 0, width: self.view.frame.width, height: 80))
label.text = "第\(indexPath.row)行"
cell.addSubview(label)
return cell
}
3.表格组的设置
//有多少组
func