TableViewCell添加和删除

延展声明

{

    UITableViewCellEditingStyle _style;

}



添加按钮

 
    //第一步:让tableView处于编辑状态
   // self.rv.table.editing=YES;
    
    UIBarButtonItem *b1=[[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleDone target:self action:@selector(deleteBarButton:)];
    self.navigationItem.leftBarButtonItem=b1;
    
    UIBarButtonItem *b2=[[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStyleDone target:self action:@selector(addBarButton:)];
    self.navigationItem.rightBarButtonItem=b2;
    

按钮方法

//设置按钮编辑
-(void)deleteBarButton:(UIBarButtonItem *)sender{

    //点击删除按钮式给一个删除状态
    _style=UITableViewCellEditingStyleDelete;

    BOOL flag=self.rv.table.editing;
    [self.rv.table setEditing:!flag animated:YES];
  
    

}
//这只添加按钮
-(void)addBarButton:(UIBarButtonItem *)sender{
    
    
    _style=UITableViewCellEditingStyleInsert;
    
    BOOL flag=self.rv.table.editing;
    [self.rv.table setEditing:!flag animated:YES];
    
    
    
}


方法

//指定哪些行可以被编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    //如果不重写这个方法.默认全部cell被编辑
//    if (indexPath.row %2) {
//        return YES;
//    }
    return YES;

}
//确定编辑样式 (添加删除)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{



    return  _style;

}

//完成编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if (editingStyle ==UITableViewCellEditingStyleDelete) {
        //删除数据源
        [self.groupArray[indexPath.section] removeObjectAtIndex:indexPath.row];
        
        
        
        //修改UI
        // [tableView reloadData];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
 
    }
    
    else{
    
        //修改数据源
        NSString *string=@"测试数据";
       // [self.groupArray[indexPath.section] addObject:string];
        [self.groupArray[indexPath.section] insertObject:string atIndex:indexPath.row+1];
        
      //  [tableView reloadData];
        
        NSIndexPath *index=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
        
        [tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];
        
    }
    
    
    
}


//指定哪些行可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{


    return YES;


}

//移动完成
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    //更新数据源
    //记录
    NSString *s=[NSString stringWithFormat:@"%@",self.groupArray[sourceIndexPath.section][sourceIndexPath.row]];
    
    
    //移除
    [self.groupArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];

  //添加
    [self.groupArray[destinationIndexPath.section] insertObject:s atIndex:destinationIndexPath.row];

    //更新数据
    [tableView reloadData];
    

}

//限制移动区域的
-(NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{

    //先判断源和目的的分组是否相同
    if (sourceIndexPath.section!=proposedDestinationIndexPath.section) {
        //如果不同返回源的起始位置
        return sourceIndexPath;
    }
    else{
        //如果相同,返回目的位置
        return proposedDestinationIndexPath;
    
    }




}


你可能感兴趣的:(UI)