美文网首页UIiOSiOS开发好文
Day11:UITableView(单分组, 多分组, 左滑删除

Day11:UITableView(单分组, 多分组, 左滑删除

作者: Vinc | 来源:发表于2015-05-20 21:03 被阅读1424次

UITableView(表格视图)

UITableView是用一种表格的形式来显示一组或者多组数据

UITableView继承于UIScrollView,UITableView默认值水平方向不能滚动,在垂直方向可以滚动

一、单分组的UITableView

  1. 创建数据源
  • 创建表格视图
  • 遵守协议:UITableViewDelegate、UITableViewDataSource
    • 重用标志

二、多分组的UITableView(略)

三、UITableView的删除功能

  1. 点击删除按钮调用的Delegate方法
  2. 修改删除按钮的文字

四、UITableView的插入和移动功能

  1. 如果正在编辑,点击结束编辑; 如果不在编辑,点击进入编辑状态
  2. 修改返回编辑的状态: 此处返回插入数据的状态
  3. 插入数据的操作
  4. 实现不同组不能交换的逻辑

一、单分组的UITableView

  1. 创建数据源

  • 创建表格视图

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style

  • 遵守协议:UITableViewDelegate、UITableViewDataSource

    • 协议方法中一些参数的意义

        NSIndexPath:section row
        section:表示在表格的第几组
        row:表示在表格的第几行
      
    • 重用标志
        static NSString *cellId = @"cellId";
        
        //从重用的reuseArray里面获取
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        //没有获取到重用的cell,就需要创建一个新的cell
        if (nil == cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            NSLog(@"%ld",indexPath.row);
        }
      

二、多分组的UITableView


三、UITableView的删除功能:左滑出现删除按钮

  1. 点击删除按钮调用的Delegate方法: (实现该方法就能左滑出现"删除"按钮)

     /*
      第一个参数:表格视图对象
      第二个参数:编辑表格的方式
      第三个参数:操作的cell对应的位置
      */
     - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
     {
         //如果是删除
         if (editingStyle == UITableViewCellEditingStyleDelete) {
             
             //点击删除按钮调用这里的代码
             //数据源删除
             NSMutableArray *subArray = _dataArray[indexPath.section];
             [subArray removeObjectAtIndex:indexPath.row];
             
             //UI的删除
             //删除表格视图的某一个cell
             /*
              第一个参数:将要删除的所有cell的indexPath组成的数组
              第二个参数:动画
              */
             //@[indexPath]等价于
             //[NSArray arrayWithObjects:indexPath, nil];
              [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
             //将整个表格视图刷新
             //[tableView reloadData];       
         }
     }  
    
    • 默认实现了下述方法,默认返回YES(因此可以不实现)
      - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  2. 修改删除按钮的文字

     - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
     {
         return @"删除";
     }
    

四、UITableView的插入和移动功能

  1. 如果正在编辑,点击结束编辑; 如果不在编辑,点击进入编辑状态

[_tbView setEditing:!_tbView.editing animated:YES];

  1. 修改返回编辑的状态: 此处返回插入数据的状态

默认返回UITableViewCellEditingStyleDelete
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleInsert;
}

  1. 插入数据的操作

     -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
     {
         if (editingStyle == UITableViewCellEditingStyleInsert) {
             //如果是插入
             //点击添加按钮的操作
             NSMutableArray *subArray = _dataArray[indexPath.section];
             
             //添加一条数据
             StudentModel *model = [[StudentModel alloc] init];
             model.name = @"我是新同学";
             model.age = 18;
             [subArray insertObject:model atIndex:indexPath.row];
             
             //刷新表格
             [_tbView reloadData];
             
         }
     }
    
  2. 实现不同组不能交换的逻辑

     - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
     {
         if (sourceIndexPath.section!=proposedDestinationIndexPath.section) {
             return sourceIndexPath;
         }
         return proposedDestinationIndexPath;
     }
    

相关文章

网友评论

  • 90后的晨仔:你好,我是自定义的SenctionHeaderView,请问有什么方法可以实现侧滑删除SenctionHeaderView吗?
    Vinc:@屌丝爷霉儿 据我目前掌握的知识,好像没有

本文标题:Day11:UITableView(单分组, 多分组, 左滑删除

本文链接:https://www.haomeiwen.com/subject/llblqttx.html