UITableViewCell的移动、插入与删除

当我们的工程中需要动态插入或者删除UITableViewCell时,我们该如何做呢?

如果要实现这些操作,前提要实现移动单元格就需要把单元格的编辑属性设置为YES,有两种方式,第一种是改变tableview的属性:[tableView setEditing:YES animated:YES];,第二种通过代理的方式:

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

1.移动

//返回YES,表示支持单元格的移动  
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    return YES;  
}  
//单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格  
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    return UITableViewCellEditingStyleInsert;  
} 
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath  
{  
//    需要的移动行  
    NSInteger fromRow = [sourceIndexPath row];  
//    获取移动某处的位置  
    NSInteger toRow = [destinationIndexPath row];  
//    从数组中读取需要移动行的数据  
    id object = [self.listData objectAtIndex:fromRow];  
//    在数组中移动需要移动的行的数据  
    [self.listData removeObjectAtIndex:fromRow];  
//    把需要移动的单元格数据在数组中,移动到想要移动的数据前面  
    [self.listData insertObject:object atIndex:toRow];    
}  
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSInteger from=sourceIndexPath.row;
    NSInteger to=destinationIndexPath.row;
    id obj=[self.items objectAtIndex:from];
    [self.items removeObjectAtIndex:from];
    [self.items insertObject:obj atIndex:to];
}

2.删除

首先是判断(UITableViewCellEditingStyle)editingStyle

//单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格  
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    return UITableViewCellEditingStyleDelete;  
}  
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    if (editingStyle==UITableViewCellEditingStyleDelete) {  
//        获取选中删除行索引值  
        NSInteger row = [indexPath row];  
//        通过获取的索引值删除数组中的值  
        [self.listData removeObjectAtIndex:row];  
//        删除单元格的某一行时,在用动画效果实现删除过程  
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
    }  
}  

3.插入

实现方法和删除方法相同,首先还是返回单元格编辑风格

/单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格  
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    return UITableViewCellEditingStyleInsert;  
}  
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    if (editingStyle==UITableViewCellEditingStyleDelete) {  
//        获取选中删除行索引值  
        NSInteger row = [indexPath row];  
//        通过获取的索引值删除数组中的值  
        [self.listData removeObjectAtIndex:row];  
//        删除单元格的某一行时,在用动画效果实现删除过程  
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
    }  
    if(editingStyle==UITableViewCellEditingStyleInsert)  
    {  

        i=i+1;  
        NSInteger row = [indexPath row];  
        NSArray *insertIndexPath = [NSArray arrayWithObjects:indexPath, nil];  
        NSString *mes = [NSString stringWithFormat:@"添加的第%d行",i];  
//        添加单元行的设置的标题  
        [self.listData insertObject:mes atIndex:row];  
        [tableView insertRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationRight];  
    }  
}  

4.推荐一种已经实现好的可以滑动出现效果的:SWTableViewCell

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值