zoukankan      html  css  js  c++  java
  • UITableView编辑

     
    UITableView 编辑步骤如下:
    1.让TableView处于编辑状态
    2.协议设定
     2.1.确定Cell是否处于编辑状态
     2.2.设定Cell的编辑样式(删除、添加)
     2.3.编辑状态进⾏提交
    注意: 编辑结束后,由于numberOfRowsInSection这个协议只在 tableview添加到⽗视图的时候⾛⼀次,⽽且table上的数据 都是由数组提供,因此,需要先将数组中的元素删除,然后 让table的协议重新⾛⼀遍进⾏重新赋值。 即:先修改数据源,再刷新table(使⽤reloadData⽅法)
     
    //每一个视图控制器都有一个编辑按钮,因为项目中编辑的应用场景非常多,所以系统预留了一个编辑按钮供我们使用
        self.navigationItem.leftBarButtonItem = self.editButtonItem;
     
    #pragma mark -----删除、添加数据-----

    //1、让将要执行删除、添加操作的表视图处于编辑状态
    - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
        //先执行父类中的这个方法
        [super setEditing:editing animated:animated];
       
        //表视图执行此方法
        [self.tableView setEditing:editing animated:animated];
    }

    //2、指定表视图中哪些行可以处于编辑状态
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    //    if (indexPath.row % 2 == 0) {
    //        return YES;
    //    }
    //    return NO;
        //默认全部都可以进行编辑
        return YES;
    }

    //3、指定编辑样式,到底是删除还是添加
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        //如果此方法不重写,默认是删除样式
        return UITableViewCellEditingStyleInsert;
    //    return UITableViewCellEditingStyleDelete;
    //    if (indexPath.row > 10) {
    //        return UITableViewCellEditingStyleInsert;
    //    } else {
    //        return UITableViewCellEditingStyleDelete;
    //    }
    }
    //4、不管是删除还是添加,这个方法才是操作的核心方法,当点击删除、或者添加按钮时,需要做什么事情,怎样才能完成删除或者添加操作,全部在这个方法内部指定
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        [tableView beginUpdates];//表视图开始更新
       
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            //1、将该位置下的单元格删除
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
           
            //2、删除数据数组中,与该单元格绑定的数据
            [_dataArray removeObjectAtIndex:indexPath.row];
        } else if (editingStyle == UITableViewCellEditingStyleInsert) {
            Student *student = _dataArray[indexPath.row];
           
            //获取一个位置信息
            NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
            [tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];
            [_dataArray insertObject:student atIndex:index.row];
        }
       
        [tableView endUpdates];//表视图结束更新
    }
     
      #pragma mark -----表视图移动操作-----
    //1、移动的第一步也是需要将表视图的编辑状态打开(上面已写)
    //2、指定哪些行可以进行移动
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
        //默认是都可以移动
        return YES;
    }

    //3、移动完成之后要做什么事情,怎么完成移动
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
        //先记录原有位置下的模型数据
        Student *student = _dataArray[sourceIndexPath.row];
        [student retain];//防止野指针
        //删除原位置下的模型数据
        [_dataArray removeObjectAtIndex:sourceIndexPath.row];
        //在新位置将记录的模型数据添加到数据组中
        [_dataArray insertObject:student atIndex:destinationIndexPath.row];
        [student release]; 
    }
     
    #pragma mark -----代理协议-----
    //点击单元格触发的方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        //push操作、传值
        DetailViewController *detailVC = [[DetailViewController alloc] init];
        detailVC.student = _dataArray[indexPath.row];
        [self.navigationController pushViewController:detailVC animated:YES];
        [detailVC release];
    }
     
    ····································
    二、UITableViewController
     
    UITableViewController是继承于UIViewController中的一个类,只不过比UIViewController中多了一个属性tableView。即:UITableViewController是一个自带table的视图控制器
    self.view不是UIView而是UITableView
    datasource和delegate默认都是self(UITableViewController)
    开发中只需要建立UITableViewController子类
     
    /*
     1、表视图控制器自带的根视图为UITableView对象
     2、self.tableView就代表根视图
     3、创建出来的表视图控制器 已经 自动接收了 数据源协议和代理协议
     4、不需要通过代码建立协议与代理关系
     5、表视图控制器.m文件已经自动帮你添加了数据源协议中必须实现的方法
     */
     
  • 相关阅读:
    Java基础教程
    一个RDBMS左连接SQL执行计划解析
    hive时间日期函数及典型场景应用
    ETL数据采集方法
    数据仓库保存历史数据方法之拉链表
    NAS服务器局域网内IPad、手机、电视盒子等联网播放
    转:主流数据恢复软件——EasyRecovery/Ashampoo Undeleter/Wise Data Recovery/Recuva/Undelete 360
    [转]office2010一直卡在“正在受保护的视图中打开”
    [转]PROE传动链条的装配教程
    linux下svn定时更新项目
  • 原文地址:https://www.cnblogs.com/Walking-Jin/p/5210891.html
Copyright © 2011-2022 走看看