UITableView
2015-12-05 本文已影响226人
ly渐行渐远
UITableView
UITableView继承自UIScrollView,所以可以滚动。表视图的每一条数据都是显示在UITableViewCell对象中。表视图可以分区显示数据,每个分区称为一个section,每一行称为row,编号都是从0开始。
初始化:
tableView.backgroundColor = [UIColor whiteColor];//设置背景色
//指定代理
tableView.dataSource = self;
tableView.delegate = self;
//设置分割线样式
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//设置分隔线颜色
tableView.separatorColor = [UIColor redColor];
[self.view addSubview:tableView];```
##UITableViewDataSource代理方法
tableView: numberOfRowsInSection:设置每个分区的cell的个数(必须实现)
```- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;//每个分区下有10个cell
}```
tableView:cellForRowAtIndexPath:定义cell(必须实现)
```- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//初始化一个tableViewCell
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
//设置tableViewCell的标题
cell.textLabel.text = @"cell";
//设置tableViewCell的副标题
cell.detailTextLabel.text = @"name";
//设置图片
cell.imageView.image = [UIImage imageNamed:@"1.png"];
return cell;
}```
numberOfSectionsInTableView:设置共有多少个分区
```- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;//共有两个分区
}```
sectionIndexTitlesForTableView:为表示图添加索引条
```- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *array = @[@"1",@"2"];
return array;
}```
tableView:titleForHeaderInSection:通过代理为每一个分区添加标题
```- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"a";
}```
tableView: canEditRowAtIndexPath:设置可编辑状态下得表视图那个单元格课编辑
```- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//不想首个单元格可编辑。直接返回YES,默认所有单元格都可编辑
if (indexPath.row == 0)
{
return 0;
}
}```
tableView: canMoveRowAtIndexPath:单元格是否可移动
```- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
//YES默认所有单元格都可以移动
return YES;
}```
tableView:commitEditingStyle:forRowAtIndexPath:编辑完成之后所执行的代理方法
```- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除单元格
/**
* indexPaths:次参数为数组,数组中的元素为NSIndexPath类型,说明此方法可以删除多个单元格
* Animation:删除动作的动画
*/
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
//如果编辑样式为插入时要做的操作
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
//增加一个单元格
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
else
{
}
}```
tableView:moveRowAtIndexPath: toIndexPath:移动完成会执行的代理方法,sourceIndexPath当前你所移动的单元格原来的位置,destinationIndexPath 移动完成后所在的新位置
```- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//把要移动位置的数据先保存起来
NSString *string = [NSString stringWithString:[self.dataMutableArray objectAtIndex:sourceIndexPath.row]];
//删除原来位置的数据
[self.dataMutableArray removeObjectAtIndex:sourceIndexPath.row];
//把此数据插入到新的位置
[self.dataMutableArray insertObject:string atIndex:destinationIndexPath.row];
}```
##UITableViewDelegate代理方法
tableView:didSelectRowAtIndexPath:点击cell响应的代理方法
```- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld",indexPath.row);
}```
tableView:heightForRowAtIndexPath:通过代理设置cell的高度
```- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}```
tableView:editingStyleForRowAtIndexPath:设置编辑样式
```- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//让最后一行为插入数据样式
if (indexPath.row == self.dataMutableArray.count - 1) {
return UITableViewCellEditingStyleInsert;
}
//默认所有单元格都是删除样式
return UITableViewCellEditingStyleDelete;
}```