作用 :实现以一列多行的形式来显示大量的数据的一种视图
表格的样式 (style) : 行之间没有间距,普通样式:plain 可以将行分组(区),分组样式:Group
表格的添加: 1.声明一个tableview的属性
@property(nonatomic,strong)UITableView *tableview;2.初始化并添加到父视图中
- (void)viewDidLoad { - self.tableView=[[UITableView alloc]initWithFrame:<#(CGRect)#> style:<#(UITableViewStyle)#>]; -//设置代理和数据源为自己 - self.tableview.datasoure=self; - self.tableview.delegate=self; - [self.view addSubview:self.tableView]; - }3.采纳协议
@interface TestViewController ()<UITableViewDataSource,UITableViewDelegate>4. 实现方法 UITableViewDataSource
俗称“3问一答”
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; //一共有几个分区 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; //一个分区有几个Row - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; //每个Row是什么样子的4.实现代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; - //调整Row的高度 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; -//点中某行如何处理等等方法可以进入<UITableViewDelegate>查看
使用 : 新建一个类,继承UITableViewController. 只需要关注“三问一答”即可
@interface HomeTableViewController : UITableViewController通过Cell获取对应的indexPath NSIndexPath *index = [tableView indexPathForCell:cell];
系统版 已经提供了是哪个可用的控件,用于显示文字或图片 通过cell 的textLabel detailTextLabel imageView 创建cell时,通过style参数限定三个控件的摆放位置 系统的四种方式 : Default:没有详细 Value1:详细在右边 Value2:没有imageview Subtitle:详细在下面
自定义 1.创建要显示的控件 2.将创建好的空间以子视图的形式,添加 到cell.contentView中即可
系统版 通过cell 的accessoryType属性设置 checkmark 对勾 disclosurelndicator 大于号 detailButton 圆圈i detailDiscloureButton 圆圈i+大于号 自定义 1.创建控件实例 2.将创建好的控件实例 赋值 给cell.accessoryView即可
核心:如果没有取出,自己创建 原理:系统会将超出屏幕的不可见单元格 放回到tableView的一个队列中存储,在需要一个cell对象先尝试从队列中取,如有有则把这个cell从队中取出继续使用,如果没有则创建新的cell
重用方式二: 核心:如果没有取出,系统自动创建 原理:在开始的时候,向系统注册一个cell类型的样式. 在需要一个cell对象先尝试从队列中取,如有有则把这个cell从队中取出继续使用,如果没有 系统会根据之前注册的样式创建一个cell使用
表格结合各种数据模型的显示: [前提:表格的行数是不定的]