AppDelegate.swift:
将导航视图控制器作为根视图:
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
self.window?.makeKeyAndVisible()
//创建导航视图控制器的根视图
let vc = RootTableViewController()
//2.创建导航视图控制器,并为她制定根视图控制器
let navigation = UINavigationController(rootViewController: vc)
//3.将导航视图控制器设置为window的根视图控制器
self.window?.rootViewController = navigation
Customcell.swift:(自定义的cell)
//屏幕的宽
let kScreenWidth = UIScreen.main.bounds.size.width
//屏幕的高
let kScreenHeigh = UIScreen.main.bounds.size.height
class Customcell: UITableViewCell {
var namelabel:UILabel!
var phonelabel:UILabel!
var agelabel:UILabel!
//一个类A如果有自己独有的初始化方法,自定义类B的时候如果B继承自A。此时就重写A类独有的初始化方法
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier:reuseIdentifier )
self.setupViews()
}
func setupViews() {
//namelabel
self.namelabel = UILabel(frame: CGRect(x: 10, y: 5, width: kScreenWidth-20, height: 40))
namelabel.backgroundColor = UIColor.red
//cell在自定义视图的时候把自定义的控件加到contentView上
self.contentView.addSubview(namelabel)
//phonelabel
phonelabel = UILabel(frame: CGRect(x: 10, y: 50, width: kScreenWidth-20, height: 40))
phonelabel.backgroundColor = UIColor.green
self.contentView.addSubview(phonelabel)
//agelabel
agelabel = UILabel(frame: CGRect(x: 10, y: 95, width: kScreenWidth-20, height: 40))
agelabel.backgroundColor = UIColor.yellow
self.contentView.addSubview(agelabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
RootTableViewController.swift:
注册cell:
//重用标识
let CustomcellReuseidentifier = "Customcell"
override func viewDidLoad() {
super.viewDidLoad()
//注册cell
self.tableView.register(Customcell.self, forCellReuseIdentifier: CustomcellReuseidentifier)
}
需要实现的方法
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//根据重用标识区出来的cell要转为注册的cell类型,才能 点 出来他的属性
let cell = tableView.dequeueReusableCell(withIdentifier: CustomcellReuseidentifier, for: indexPath) as! Customcell
cell.namelabel.text = "张三"
cell.phonelabel.text = "66666666"
cell.agelabel.text = "18"
return cell
}
//cell 高度的设置
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 145
}