import UIKit
//获取屏幕的宽和高
let kScreenWidth = UIScreen.main.bounds.width
let kScreenheight = UIScreen.main.bounds.height
class ViewController: UIViewController,UITableViewDataSource {
var tableView:UITableView!
//重写lodeView
override func loadView() {
self.tableView = UITableView(frame: UIScreen.main.bounds, style: .grouped)
self.view = tableView
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = 184
tableView.separatorStyle = .singleLine
tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
tableView.separatorColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 150))
headerView.backgroundColor = #colorLiteral(red: 1, green: 0.7192476913, blue: 0.7031846319, alpha: 1)
tableView.tableHeaderView = headerView
tableView.tableFooterView = UIView()
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 100
}
//UITableViewCell 的重用
//1先去重用池中找对应重用标识的cell,如果找不到就自己创建,找得到就直接拿来用
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
/*
let cell = UITableViewCell(style: .value2, reuseIdentifier: nil)
//indexPath存储的是cell所在分区的下标以及分区中cell的下标
//indexPath: section存储是分区下标
//roe:存储是cell所在分区中的下标
cell.textLabel?.text = "分区\(indexPath.section)行号\(indexPath.row)"
cell.detailTextLabel?.text = "detailTextLabel"
cell.imageView?.image = #imageLiteral(resourceName: "poto.png")
let accView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 40))
accView.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
cell.accessoryView = accView
return cell*/
//1 准备重用的标识(重用池使用集合NSSet做的)
let indentifer = "cell"
//2tableView去重用池中根据重用标识取cell
var cell = tableView.dequeueReusableCell(withIdentifier: indentifer)
//如果cell为空 就自己创建
if cell == nil{
cell = UITableViewCell(style: .default, reuseIdentifier: indentifer)
print("创建cell")
}
cell?.textLabel?.text = "行号\(indexPath.row)"
return cell!
}
func numberOfSections(in tableView: UITableView) -> Int{
return 5
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "区头标题\(section)"
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "区尾标题\(section)"
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return ["0","1","2","3","4"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
网友评论