iOS UITableView的编辑方法

本文详细介绍了iOS中UITableView的编辑和删除功能,包括如何实现删除操作的确认、每一行的编辑样式判断、编辑状态的设置以及哪一行可以进行编辑。通过实例代码深入解析了这些功能的具体实现。

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

iOS 的UITableView 会有一些代理方法,这里时编辑和删除的一些简单的方法


// 确认编辑 实现的一些方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   
if(editingStyle == UITableViewCellEditingStyleDelete)
    {
       
NSString *key = self.array[indexPath.section];
       
NSMutableArray *array = self.dic[key];
       
NSLog(@"%ld",array.count);
 
         [PerSonDate dictionaryDeleteDate:array[indexPath.row]];
       
        
self.dic = [PerSonDate shareDictionaryDate].date;
        
self.array = [PerSonDate shareDictionaryDate].array;
 
       
      
if(self.array.count != count)
        {
            [
self.tableView reloadData];
           
count = (int)self.array.count;
        }
       
else
        {
           
NSArray *aa =  @[indexPath];
           [
self.tableView deleteRowsAtIndexPaths:aa withRowAnimation:UITableViewRowAnimationLeft];
        }
    

       
   
    }
}

//每一行的编辑样式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
if (indexPath.section == 0) {
       
       
return UITableViewCellEditingStyleDelete;
    }
   
   
return UITableViewCellEditingStyleInsert;
}

// Yes编辑和 NO不编辑

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [
super setEditing:editing animated:animated];
   
   
//[self.tableView setEditing:YES animated:YES];
   
   
   
if(flag == 0)
    {
       
    [
self.tableView setEditing:YES animated:YES];
       
flag = 1;
    }
   
   
else  if(flag == 1)
     {
          [
self.tableView setEditing:NO animated:YES];
        
flag = 0;
     }
}


// 哪一行可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
   
if (indexPath.section == 1  && indexPath.row == 0) {
       
return YES;
    }
   
return YES;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值