美文网首页
tableView 重用机制

tableView 重用机制

作者: 陆小琪 | 来源:发表于2016-11-08 14:02 被阅读0次

importUIKit

classViewController:UIViewController{

overridefuncviewDidLoad() {

super.viewDidLoad()

//style:(1).Plain分区之间没有间距(2).Group分区之间有间距

lettableView:UITableView=UITableView(frame:view.bounds, style: .Plain)

//提供视图相关操作

tableView.delegate=self

//设置数据源代理(负责提供数据)

tableView.dataSource=self

view.addSubview(tableView)

//给tableView注册cell,当有cell滑出屏幕的时候会将单元格cell放到缓存池中并且给上重用标识符cell

tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell")

}

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

//遵循多个协议采用,隔开

extensionViewController:UITableViewDelegate,UITableViewDataSource{

//返回每个分区的行数

functableView(tableView:UITableView, numberOfRowsInSection section:Int) ->Int{

return1000

}

//返回每个单元格,单元格:UITableViewCell,NSIndexPath是存储该单元格是第几分区·第几行

functableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) ->UITableViewCell{

//每一个单元格对应着一个UItableViewCell,其中封装了三个属性:imageView,textLabel,detailLabel

//let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")

//tableView根据重用标识符“cell”到缓存池中查找有没缓存的cell,有的话取出来使用,没有的话创建

letcell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

//标题视图textLabel

cell.textLabel?!.text="老司机"

//副标题视图:detailLabel

cell.detailTextLabel?!.text="带带我"

//图片视图

cell.imageView?!.image=UIImage(named:"icon.png")

returncellas!UITableViewCell

}

}

相关文章

网友评论

      本文标题:tableView 重用机制

      本文链接:https://www.haomeiwen.com/subject/hvvauttx.html