8.2 UiTabelView
整個Tabelview有頭部,和尾部。里面可能有數(shù)個分組section,分組中又有分組頭部和分組尾部,分組中又有很多行Cell
1.行高,默認44個像素
2.行數(shù)
3.每一行的內(nèi)容(數(shù)據(jù))
tableView,Cell ,Row,Section,主線程,加載數(shù)據(jù)
- 關鍵詞:tableView header footer section Cell /Row枚舉類型
- Cell、Header、Footer寬度一定與TableView相同x/y/width無效(TableView中)3. 同一個Cell對象會重復使用,在隊列中獲取空閑的Cell
var cell = tableView.dequeueReusableCellWithIdentifier("cell") - 幾個關鍵函數(shù):numberOfSectionsInTableViewnumberOfRowsInSectioncellForRowAtIndexPathdidSelectRowAtIndexPathviewForHeaderInSectionheightForHeaderInSection
- 建立一個類比建立數(shù)組或者字典要優(yōu)越的多,可以在敲代碼的時候彈出類中的成員變量比如: var channel_id: String!var channel_name: String!
- 在refreshTableView放入主線程中執(zhí)行方法一:self.tableView.performSelectorOnMainThread(#selector(self.tableView.self.reloadData), withObject: nil, waitUntilDone: false)方法二:self.performSelectorOnMainThread(#selector(self.refreshTableView), withObject: nil, waitUntilDone: true)
- tableView.reloadData() //重新加載
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var names: [String]? //定義一個數(shù)組
override func viewDidLoad() {
super.viewDidLoad()
names = ["張三", "李四", "王五", "趙六"]
//.Plain樣式默認沒有分隔, .Grouped有分隔
let tableView = UITableView(frame: self.view.bounds, style: .Grouped)
tableView.dataSource = self
tableView.delegate = self
self.view.addSubview(tableView)
//Cell、Header、Footer寬度一定與TableView相同
//x/y/width無效,只有高度height有效
let headView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 50))
headView.backgroundColor = UIColor.redColor()
tableView.tableHeaderView = headView //定義頭部為紅色,50像素高
let footView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 50))
footView.backgroundColor = UIColor.greenColor()
tableView.tableFooterView = footView //定義尾部為綠色,50像素高
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
//詢問某個section中有多少條數(shù)據(jù)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return names!.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
//同一個Cell對象會重復使用
//1. 在隊列中獲取空閑的Cell
var cell = tableView.dequeueReusableCellWithIdentifier("cell")
if cell == nil {
//2. 創(chuàng)建可以重用的Cell對象
cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
}
//3. 設置內(nèi)容
cell?.textLabel?.text = names![indexPath.row]
// cell?.detailTextLabel?.text = "xxxxx"
return cell!
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
print(indexPath.section, indexPath.row)
print(names![indexPath.row])
}
//
// func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
// return "Section \(section) Header" //顯示Section頭部名稱
// }
//
// func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
// return "Section \(section) Footer" //顯示Section尾部名稱
// }
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) ->UIView? {
let v = UIView()
v.backgroundColor = UIColor.blueColor()
return v //定義Section的頭部為藍色
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) ->CGFloat {
return 44.0 //定義Section的頭部為44個像素高
}
}