//cell的屬性的衍射
//ViewController.swift
importUIKit
classViewController:UIViewController{
letnames = ["張三","李四","王五","趙六","徐七"]
varkeys = ["F","G","W","Q","Z"]
varclaaamateArrary:[[String]] = [["方亮","方了"],["郭黃婷","桂程鵬"],["吳鎮(zhèn)翦","王梨慧"],["邱青苗","邱淑貞"],["鐘偉初","周旭凱","周杰"]]
// let classmateDict = ["F":["方亮","方了"],"G":["郭果","郭子"]]
overridefuncviewDidLoad() {
super.viewDidLoad()
//style:(1).plain:分區(qū)之間沒有間距
//(2).ground:分區(qū)之間有間距
lettableView:UITableView=UITableView(frame:view.bounds, style:UITableViewStyle.Plain)
//提供視圖相關(guān)操作
tableView.dataSource=self
//設(shè)置數(shù)據(jù)源代理:(負責(zé)提供數(shù)據(jù))
tableView.delegate=self
view.addSubview(tableView)
//給tableView注冊cell,當(dāng)有cell滑出屏幕的時候會將單元格cell放到緩存池中并且給上重用標(biāo)識符cell
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell")
}
overridefuncdidReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
//遵循多個協(xié)議采用逗號“,”隔開
extensionViewController:UITableViewDelegate,UITableViewDataSource{
//返回每個分區(qū)的行數(shù)
functableView(tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
returnclaaamateArrary[section].count
}
//返回每個單元格,單元格:UITableViewCell,NSIndexPath是存儲該單元格是第幾分區(qū)(section)·第幾行
functableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) ->UITableViewCell{
//每一個單元格對應(yīng)著一個UITableViewCell,其中封裝了三個屬性:imageView,textLabel,detailLabel
//let cell = UITableViewCell()
// let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
//tableView根據(jù)重用標(biāo)識符“cell”到緩存池中查找有沒有緩存的cell,有的話取出來,沒有的話新建
letcell = tableView.dequeueReusableCellWithIdentifier("cell")as!UITableViewCell
//標(biāo)題視圖textLabel
//先獲取整個分區(qū)的數(shù)據(jù)屬于第幾個分區(qū)
letarray =claaamateArrary[indexPath.section]
//再根據(jù)該分區(qū)第幾行(indexPath.row)來獲取具體哪條數(shù)據(jù)
letvalue = array[indexPath.row]
cell.textLabel?.text= value
//副標(biāo)題視圖:detailTextLabel
cell.detailTextLabel?.text="帶帶我"
//圖片視圖
ifindexPath.row==4{
cell.imageView?.image=UIImage(named:"2.gif")
}else
{
cell.imageView?.image=UIImage(named:"1.png")
}
returncell
}
//返回分區(qū)個數(shù)
funcnumberOfSectionsInTableView(tableView:UITableView) ->Int{
//先獲取到key值數(shù)組
//let keys = classmateDict.keys
//key值數(shù)組的個數(shù)就是分區(qū)的個數(shù)
returnkeys.count
}
//返回區(qū)頭標(biāo)題
functableView(tableView:UITableView, titleForHeaderInSection section:Int) ->String? {
returnkeys[section]
}
//返回區(qū)頭索引
funcsectionIndexTitlesForTableView(tableView:UITableView) -> [AnyObject]! {
returnkeys
}
functableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath) ->CGFloat{
ifindexPath.section==0{
return50
}elseifindexPath.section==1{
return80
}else{
return150
}
}
}