iOS学习笔记2—关于tableView的一些简单操作

本文详细介绍了如何在UITableView中实现删除、移动、插入和标记行的操作,并提供了具体的代码实现。

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

1.删除:

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

{
     if (editingStyle == UITableViewCellEditingStyleDelete)
     {
         NSLog(@"....");
         NSUInteger row = indexPath.row;
         [self.editArray removeObjectAtIndex:row];
         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:(UITableViewRowAnimationTop)];
    }
    
     //收藏数据更新
 //   [self replaceCurrentViewData:self.editArray];
    

}

  实现以上函数即可

2;移动:

 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    if (sourceIndexPath != destinationIndexPath)
    {
        id object = [self.editArray objectAtIndex:sourceIndexPath.row];
        [object retain];
        [self.editArray removeObjectAtIndex:sourceIndexPath.row];
        if (destinationIndexPath.row > [self.editArray count])
        {
            [self.editArray addObject:object];
        }
        else {
            [self.editArray insertObject:object atIndex:destinationIndexPath.row];
        }
        [object release];
    }
  //  [self replaceCurrentViewData:self.editArray];
}

3:插入

这个与删除行类似。实现下面,点击 +号即可。

3.1 首先将editingStyleForRowAtIndexPath方法中的UITableViewCellEditingStyleDelete修改成UITableViewCellEditingStyleInsert

  -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (editingStyle == UITableViewCellEditingStyleDelete)
     {
         NSLog(@"....");
         NSUInteger row = indexPath.row;
         [self.editArray removeObjectAtIndex:row];
         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:(UITableViewRowAnimationTop)];
    }
    else

  {

      NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];  


  •     //同样,将数据加到list中,用的row  
  •     [self.list insertObject:@"新添加的行" atIndex:row];  
  •     [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];


       } 

        //收藏数据更新
     //   [self replaceCurrentViewData:self.editArray];
        

    }


    4:标记:

    1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    2. {   
    3.     UITableViewCell *oneCell = [tableView cellForRowAtIndexPath: indexPath];  
    4.     if (oneCell.accessoryType == UITableViewCellAccessoryNone) {  
    5.         oneCell.accessoryType = UITableViewCellAccessoryCheckmark;  
    6.     } else   
    7.         oneCell.accessoryType = UITableViewCellAccessoryNone;  
    8.     [tableView deselectRowAtIndexPath:indexPath animated:YES];   


    1. else {  
    2.     //我们实现的是在所选行的位置插入一行,因此直接使用了参数indexPath  
    3.     NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];  
    4.     //同样,将数据加到list中,用的row  
    5.     [self.list insertObject:@"新添加的行" atIndex:row];  
    6.     [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];  
    7. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值