UI控件之UITableView的协议方法

<UITableViewDataSource,UITableViewDelegate>

//设置表视图的编辑状态

-(void)setEditing:(BOOL)editing animated:(BOOL)animated

{

    [super setEditing:editing animated:animated];

    [_tableView setEditing:editing animated:animated];

}

//设置编辑模式(插入、删除、选择),默认是删除

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

{

    return UITableViewCellEditingStyleInsert;

}

//设置编辑内容(插入、删除

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

{

    if(editingStyle==UITableViewCellEditingStyleDelete)

    {

        //如果是删除的话。。。。

    }

    if(editingStyle==UITableViewCellEditingStyleInsert){

        //将数据加入到数据源

        [_dataArray insertObject:@"test" atIndex:indexPath.row];

        //更新界面

        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

    }

}

//设置是否允许移动单元格

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

{

    return YES;

}

//移动单元格执行的操作第1个参数是操作的表视图对象,第2个参数是单元格原来所在的行,第3个参数是单元格移动后新的行

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

{

    //修改数据源,将moveRowAtIndexPath所在的数据删除,插入到destinationIndexPath所在的位置上

    NSString *str=_dataArray[sourceIndexPath.row];

    [_dataArray removeObjectAtIndex:sourceIndexPath.row];

    [_dataArray insertObject:str atIndex:destinationIndexPath.row];

}

//当选择一行数据,当表视图处于编辑状态时将其数据添加到删除数组中

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

{

    if(_tableView.editing){

        NSString *str=_dataArray[indexPath.row];

        if(![_removeArray containsObject:str]){

            [_removeArray addObject:str];

        }

    }

}

//当用户取消选择某行数据时,将该行数据从删除数组中移除

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

{

    //判断表视图是否处于编辑状态

    if(_tableView.editing){

        NSString *str=_dataArray[indexPath.row];

        if([_removeArray containsObject:str]){

            [_removeArray removeObject:str];

        }

    }

}

//设置编辑模式(选择)

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

{

    //插入和删除一起设置效果为选择模式

    return UITableViewCellEditingStyleInsert|UITableViewCellEditingStyleDelete;

}

//返回分组数,默认是1

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

//返回cell的个数

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

{

    return _dataArray.count;

}

//设置每个cell的内容

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

{

    static NSString *identifier=@"cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];

    if(cell==nil){

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }

    cell.textLabel.text=_dataArray[indexPath.row];

    return cell;

}

http://www.cnblogs.com/PaulpauL/ 版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:https://www.cnblogs.com/PaulpauL/p/4890080.html