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