iOS tableView全面详解

1.tableView有两种展现形式,UITableViewStylePlain和UITableViewStyleGrouped

创建tableView

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT-44) style:UITableViewStylePlain];

2.设置tableView的代理,必须实现的两个方法设置cell的内容和cell的高度,否则会出现崩溃的现象。

    self.table.dataSource = self;

    self.table.delegate = self;

3.如果有尾部视图或者头部视图,还要设置self.tableView.tableHeaderView和self.tableView.tableFooterView

 //显示有多少组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

//显示每组的头部

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

//显示每组的尾部

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

//显示每组的尾部标题有多高

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

      return CGFLOAT_MIN;

}

//显示每组的头部标题有多高

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

      return CGFLOAT_MIN;

}

//显示每组头标题名称

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

//显示每组尾标题名称

-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

//显示每组有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

//显示每组的行高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

//显示内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 //打开或关闭编辑功能

    if ([self.table isEditing]) {

        [self.table setEditing:NO animated:YES];

    }else{

        [self.table setEditing:YES animated:YES];

    }

//对应的行是否可以被编辑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 

//设置编辑风格

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCellEditingStyleNone,

    UITableViewCellEditingStyleDelete,删除

    UITableViewCellEditingStyleInsert添加

}

//提交编辑(左滑删除和添加)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    

    if (editingStyle == UITableViewCellEditingStyleDelete) {//删除

        [self.serviceArry removeObjectAtIndex:indexPath.row];

        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];//删除刷新

    }

    else if (editingStyle == UITableViewCellEditingStyleInsert) {//添加

       [datas insertObject:@"ab" atIndex:indexPath.row];

    [self.table insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];//添加刷新

 

    }

}

//设置是否可以移动

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

//提交移动结果

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    //先把要移动的数据保存

    id moveObj = [datas objectAtIndex:sourceIndexPath.row];

    //从数组中删除要移动的数据

    [datas removeObjectAtIndex:sourceIndexPath.row];

    //把要移动的数据插入到目标位置

    [datas insertObject:moveObj atIndex:destinationIndexPath.row];

}

     

//每行的点击事件

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

原文地址:https://www.cnblogs.com/hongyan1314/p/5790166.html