11.7TableView

//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

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容