UITableView基本使用方法
1.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource
2.然后 UITableView对象的 delegate要设置为 self。
3.然后就可以实现这些delegate的一些方法拉。
这个方法返回 tableview 有多少个section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
返回有多少个Sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
这个方法返回 对应的section有多少个元素,也就是多少行。
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
这个方法返回指定的 row 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
这个方法返回指定的 section的header view 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
这个方法返回指定的 section的footer view 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
告知系统每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
返回指定的 section 的header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
返回指定的section 的 header 的 title,如果这个section header 有返回view,那么title就不起作用了。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
返回指定的 section header 的view,如果没有,这个函数可以不返回view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
当用户选中某个行的cell的时候,回调用这个。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
但是首先,必须设置tableview的一个属性为可以select 才行。
TableView.allowsSelection=YES;
点击后显示的风格
cell.selectionStyle=UITableViewCellSelectionStyleBlue;
如果不希望响应select,那么就可以用下面的代码设置属性:
TableView.allowsSelection=NO;
如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置
cell.selectionStyle = UITableViewCellSelectionStyleNone;
如何设置tableview 每行之间的分割线
self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
如果不需要分割线,那么就设置属性为
UITableViewCellSeparatorStyleNone
即可。
如何设置 tableview cell的背景颜色
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //设置背景颜色 cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1]; }
这个函数响应,用户点击cell 右边的 箭头(如果有的话)
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
如何设置tableview 可以被编辑
首先要进入编辑模式:
[TableView setEditing:YES animated:YES];
如果要退出编辑模式,肯定就是设置为NO
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
返回当前cell 要执行的是哪种编辑,下面的代码是返回删除模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; }
通知告诉用户编辑了 哪个cell,对应下面的代码,我们在这个函数里面执行删除cell的操作。
-(void) tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { [chatArray removeObjectAtIndex:indexPath.row]; [chatTableView reloadData]; }
如何获得 某一行的CELL对象
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
tableView的常见设置
- 设置每一行cell的高度
self.tableView.rowHeight = 100;
- 设置每一组头部的高度
self.tableView.sectionHeaderHeight = 50;
- 设置每一组尾部的高度
self.tableView.sectionFooterHeight = 50;
- 设置分割线颜色
self.tableView.separatorColor = [UIColor redColor];
- 设置分割线样式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- 设置表头控件
self.tableView.tableHeaderView = [[UISwitch alloc] init];
- 设置表尾控件
self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];
- 设置右边索引文字的颜色
self.tableView.sectionIndexColor = [UIColor redColor];
- 设置右边索引文字的背景色
self.tableView.sectionIndexBackgroundColor = [UIColor blackColor];
tableViewCell的常见设置
- 设置右边的指示样式
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- 设置右边的指示控件
cell.accessoryView = [[UISwitch alloc] init];
- 设置cell的选中样式
cell.selectionStyle = UITableViewCellSelectionStyleNone; // backgroundView优先级 > backgroundColor
- 设置背景色
cell.backgroundColor = [UIColor redColor];
- 设置背景view
UIView *bg = [[UIView alloc] init]; bg.backgroundColor = [UIColor blueColor]; cell.backgroundView = bg;
- 设置选中的背景view
UIView *selectedBg = [[UIView alloc] init]; selectedBg.backgroundColor = [UIColor purpleColor]; cell.selectedBackgroundView = selectedBg;
cell的循环利用
传统的写法
每当有一个cell要进入视野范围内,就会调用一次
```- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"wine"; // 1.先去缓存池中查找可循环利用的cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.如果缓存池中没有可循环利用的cell if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; }
3.设置数据
cell.textLabel.text = [NSString stringWithFormat:@"%zd行的数据", indexPath.row];
return cell;} ```
- 新的写法(注册cell)
- 注册某个重用标识 对应的 Cell类型,优先级最高
NSString *ID = @"wine";
- (void)viewDidLoad {
``` [super viewDidLoad]; ```
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:ID]; }
- 注册某个重用标识 对应的 Cell类型,优先级最高
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ // 1.先去缓存池中查找可循环利用的cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.设置数据
cell.textLabel.text = [NSString stringWithFormat:@"%zd行的数据", indexPath.row];
return cell;
}```