iOS tableView删除,添加,排序方法实现

本文介绍在iOS开发中如何使用UITableView进行两种类型的行删除操作:滑动删除与点击按钮删除。文章详细解释了如何通过代码配置这两种删除方式,并展示了如何在删除操作完成后更新数据源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里的删除方式有两种,一种是滑动删除,一种是点击"-"号删除按钮再删除。

将下面的注释去掉后实现的是点击按钮删除,未去掉的情况下是滑动删除。

注意下面的list数组是可变数组哦!

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

    static NSString *cellId = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

    if(cell == nil){

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

    }

    cell.textLabel.text = [list objectAtIndex:indexPath.row];

    

    return cell;

    

}

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

    return [list count];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}

//没有实现该方法的时候,默认是滑动删除的

//决定单元格编辑状态

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

//    //return action == 0 ? UITableViewCellEditingStyleDelete :

//   // UITableViewCellEditingStyleInsert;

   

//}


//删除指定行时指定的确认文本

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

    return @"确定删除";

}

//DataSourse协议,决定某行是否可以编辑

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

    if(indexPath.row == 0){

        return NO;

    }else{

        return  YES;

    }

    return YES;

    

}

//DataSourse协议,移动完成时激发该方法

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

    NSInteger originRow = sourceIndexPath.row;

    NSInteger destRow = destinationIndexPath.row;

    id target = [list objectAtIndex:originRow];

    [list removeObjectAtIndex:originRow];

    [list insertObject:target atIndex:destRow];

    

}

//编辑(包括删除或插入)完成时激发该方法

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSInteger originRow = indexPath.row;

        [list removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    }else if(editingStyle == UITableViewCellEditingStyleInsert){

        //在当前行的下一行插入数据

        [list insertObject:[list objectAtIndex:indexPath.row] atIndex:(indexPath.row + 1)];

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

    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值