UITableView是UIKit中最常用的一种视图,是UIScrollView的子类
本篇文章介绍UITableView的基本使用,包括:
,,,, UITableView的数据源驱动
,,,,各种数据源,代理方法
,,,,单元格的重用机制
,,,,数据的刷新
,,,,…
- <李>
<强> UITableView的样式强>
李>创建时需要指定样式:
安康;(instancetype) initWithFrame: (CGRect中)frame 风格:(UITableViewStyle)风格 @ property(原子,只读的),UITableViewStyle 风格 typedef enum  { ,,,,UITableViewStylePlain, ,,,UITableViewStyleGrouped },UITableViewStyle;
,,,,
- <李>
<强> UITableView中的内容强>
李>表格视图中可以包含多个组
每个组中又可以包含多个单元格(细胞)
每个组上面视的头图
每个组下面视的页脚图
这些属性的赋值:使用数据源和代理驱动
- <李>
<强> UITableView的数据源驱动强>
李>UITableView包含两个代理:
@ property(原子,,分配),id<, UITableViewDelegate 祝辞,delegate ,//代理 @ property(原子,分配),id<, UITableViewDataSource 祝辞,dataSource //数据源
,,,数据源可以使用代理设计模式,其功能属于代理的第三种应用,为自身属性赋值
重要的数据源方法:
,,,表格视图中应当包含多少个组,默认为1
安康;(NSInteger) numberOfSectionsInTableView: (UITableView *) tableView
,,,表格视图中指定组中应当包含多少个细胞,必须实现
安康;(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection:也(NSInteger)部分
,,,表格视图中指定组及行的细胞视图,必须实现
安康;(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
,,,表格视图中细胞的视图在将要显示时自动调用,应将数据绑定的代码放在这里
安康;(void) tableView: (UITableView *) tableView willDisplayCell: (UITableViewCell *) cell forRowAtIndexPath: (NSIndexPath *) indexPath
,,,,一般来说,这四个数据源方法,是必须要实现的(第一个不实现默认为一个部分)
如:
//当前控制器遵循数据源,代理协议 @interface ViewController (), & lt;需要,,UITableViewDelegate>
//当前控制器成为tableView的数据源和代理 时间=self.tableView.dataSource 自我;=self.tableView.delegate 自我;
//四个数据源方法 安康;(NSInteger) numberOfSectionsInTableView:(UITableView *)视图 { ,,,return 3;//三个部分 } 安康;(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection:也(NSInteger)部分 { ,,,return 节+ 1; } 安康;(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: indexPath  (NSIndexPath *); { ,,,UITableViewCell *, cell =, [[UITableViewCell alloc], initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @“111”); ,,,return 细胞; } 安康;(void) tableView: (UITableView *) tableView willDisplayCell: (UITableViewCell *) cell forRowAtIndexPath: indexPath (NSIndexPath *) { ,,,cell.textLabel.text =, (NSString stringWithFormate: @”部分:% ld 行:% ld”,, indexPath.section,, indexpath。Row); }
- <李>
<强> UITableView的行高强>
李>两种方式:
1)统一的高度通过UITableView对象的rowHeight属性设定
@ property(原子),CGFloat rowHeight
2)也可以通过实现tableView的代理方法,返回每一行的高度
安康;(CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath