美文网首页
UITableView的编辑状态

UITableView的编辑状态

作者: Lambo316 | 来源:发表于2016-06-28 10:53 被阅读231次

    一、cell编辑步骤

    1、让tableView 处于编辑状态

    2、设置某些cell 可以编辑

    3、设置某些cell的编辑样式

    4、处理编辑结果

    //self.editButtonItem响应方法,让tableView 处于编辑状态

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

    {

    [super setEditing:editing animated:animated];

    //开启或关闭tableView的编辑状态

    [self.tableView setEditing:editing animated:YES];

    }

    //返回YES,则表示该cell可以被编辑,返回NO,则表示该cell不可以被编辑

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

    //设置编辑样式

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

    {

    /*

    Delete:删除

    Insert:添加

    Delete | Insert:多选

    */

    return UITableViewCellEditingStyleInsert;

    }

    //处理编辑结果

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

    {

    //处理删除结果

    if (editingStyle == UITableViewCellEditingStyleDelete) {

    //第一、删除数据源数据

    [self.dataArray removeObjectAtIndex:indexPath.row];

    //第二、删除cell或者更新tableView

    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

    //简单粗暴 无动画,效率低

    [self.tableView reloadData];

    }

    else if (editingStyle == UITableViewCellEditingStyleInsert)

    {

    //第一步、添加数据源数据

    Contact *con = self.dataArray[indexPath.row];

    [self.dataArray insertObject:con atIndex:indexPath.row+1];

    //第二步、添加cell或者更新tableView

    NSIndexPath *myPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];

    [self.tableView insertRowsAtIndexPaths:@[myPath] withRowAnimation:UITableViewRowAnimationBottom];

    //简单粗暴 无动画,效率低

    [self.tableView reloadData];

    }

    //设置是否可以移动

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

    //处理移动结果

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

    sourceIndexPath.row:那一行

    destinationIndexPath.row:目标行数

    相关文章

      网友评论

          本文标题:UITableView的编辑状态

          本文链接:https://www.haomeiwen.com/subject/feltjttx.html