/*
1.TableView的父類是UIScrollView庶香,但是UITableView只能上下滾動(dòng)甲棍,UITableView通過UITableViewCell展示數(shù)據(jù),UITableViewCell上有一個(gè)contentView,contentView上有一個(gè)titleLabel , detailTitleLabel , imageView , accessoryView(輔助視圖)赶掖,且UITableView可以進(jìn)入編輯模式感猛,能夠進(jìn)行增刪改
2。UITableView可以有0個(gè)或多個(gè)分區(qū)奢赂,分區(qū)是根據(jù)分區(qū)的下標(biāo)來區(qū)分陪白,每個(gè)分區(qū)可以有多條(rows)cell,cell也是根據(jù)所在分區(qū)中的下標(biāo)來區(qū)分
3. UITableView有兩種樣式plain 和 grouped,而且一旦確定下來就很難更改
4. UITableView很多方法都和NSIndexPath對(duì)象有關(guān)膳灶,要么作為方法的參數(shù)要么作為方法的返回值
*/
import UIKit
//獲取屏幕的寬和高
let kScreenWidth = UIScreen.main.bounds.width
let kScreenheight = UIScreen.main.bounds.height
class ViewController: UIViewController,UITableViewDataSource {
//定義一個(gè) TableView的屬性
var tableView:UITableView!
// 重寫lodeview方法咱士,修改視圖控制器自帶的view,將tableView作為視圖控制器的view
override func loadView() {
//創(chuàng)建UITableView對(duì)象
self.tableView = UITableView(frame: UIScreen.main.bounds, style: .grouped)
self.view = tableView
}
override func viewDidLoad() {
super.viewDidLoad()
//配置tableView的常用屬性
// rowHeight行高
tableView.rowHeight = 100
//separatorStyle 分割線樣式
tableView.separatorStyle = .singleLine
//分割線內(nèi)邊距
tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
//分割線的顏色
tableView.separatorColor = #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)
//tableHeaderView 表頭視圖,經(jīng)常用來放置輪播圖
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 150))
headerView.backgroundColor = #colorLiteral(red: 0.5843137503, green: 0.8235294223, blue: 0.4196078479, alpha: 1)
tableView.tableHeaderView = headerView
//tableFooterView 表尾視圖轧钓,經(jīng)常用來收起多余的cell
//? ? ? ? let footerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 20))
//? ? ? ? footerView.backgroundColor = #colorLiteral(red: 1, green: 0.7192476913, blue: 0.7031846319, alpha: 1)
//? ? ? ? tableView.tableFooterView = footerView
//快速收起多余的cell
tableView.tableFooterView = UIView()
//tableview的重要屬性 dataSourse,數(shù)據(jù)源代理序厉,專門負(fù)責(zé)tableView有多少個(gè)分區(qū),每個(gè)分區(qū)有多少條cell 以及返回UITableViewCell控件
tableView.dataSource = self
}
//MARK:- UITableViewDataSource協(xié)議中的方法
//返回每個(gè)分區(qū)中cell的個(gè)數(shù)
//section儲(chǔ)存的是分區(qū)的下標(biāo)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return (section % 2 == 0) ? 1 : 3
}
//返回tableViewCell的方法
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
//創(chuàng)建cell對(duì)象
//參數(shù)1:cell的樣式? 參數(shù)2:重用標(biāo)識(shí)
let cell = UITableViewCell(style: .value2, reuseIdentifier: nil)
//cell 上有一個(gè)contentView,contentView,上有textLabel,detaiTextLabel,imageView
cell.textLabel?.text = "textLabel"
cell.detailTextLabel?.text = "detailTextLabel"
cell.imageView?.image = #imageLiteral(resourceName: "poto.png")
//系統(tǒng)提供的accessoryView樣式
// cell.accessoryType = .disclosureIndicator
//賦值UIControl子類作為輔助視圖
// cell.accessoryView = UISwitch()
//自定義視圖作為輔助視圖
let accView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
accView.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
cell.accessoryView = accView
//以后如果我們要自定義cell,也要把自定義的控件添加到contentView上
return cell
}
//返回tableView中有多少個(gè)分區(qū)聋迎,默認(rèn)不設(shè)置這個(gè)方法脂矫,有一個(gè)分區(qū),但分區(qū)下標(biāo)是0
func numberOfSections(in tableView: UITableView) -> Int{
return 5
}
//返回區(qū)頭的標(biāo)題
//section 存儲(chǔ)的也是分區(qū)的下標(biāo)
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "區(qū)頭標(biāo)題\(section)"
}
//返回區(qū)尾的標(biāo)題
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "區(qū)尾標(biāo)題\(section)"
}
//返回右邊側(cè)邊欄索引的方法
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.
}
}