UIiOSiOS开发好文

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

2015-05-20  本文已影响1424人  Vinc

UITableView(表格视图)

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

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

一、单分组的UITableView

  1. 创建数据源

二、多分组的UITableView(略)

三、UITableView的删除功能

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

四、UITableView的插入和移动功能

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

一、单分组的UITableView

  1. 创建数据源

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


二、多分组的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;
     }
    
上一篇 下一篇

猜你喜欢

热点阅读