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文件已经自动帮你添加了数据源协议中必须实现的方法
 */
 

UITableView编辑的更多相关文章

  1. iOS学习30之UITableView编辑

    1. UITableView编辑 1> UITableView 编辑流程 2> UITableView 编辑步骤(四步) ① 第一步 : 让 TableView 处于编辑状态(在按钮点击事 ...

  2. IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)

    **********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...

  3. iOS学习之UITableView编辑

    一.UITableView编辑 UITableView编辑(删除.添加)步骤: 让TableView处于编辑状态. 协议设定:1)确定Cell是否处于编辑状态:2)设定cell的编辑样式(删除.添加) ...

  4. ##DAY11 UITableView编辑

    ##DAY11 UITableView编辑 每一个视图控制器都有一个编辑按钮,因为项目中编辑的应用场景非常多,所以系统预留了一个编辑按钮供我们使用 self.navigationItem.leftBa ...

  5. UI:UITableView 编辑、cell重用机制

    tableView编辑.tableView移动.UITableViewController tableView的编辑:cell的添加.删除. 使⽤场景: 删除⼀个下载好的视频,删除联系⼈: 插⼊⼀条新 ...

  6. UITableView 编辑模式(增加-删除-移动---自定义左滑 title)

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  7. UITableView编辑模式大全解

    1.UITableView 的编辑模式 进入编辑模式 代码体现 // 设置 editing 属性 tableView?.editing = true // 这个设置的时候是有动画效果的 tableVi ...

  8. iOS - UITableView 编辑(cell的插入, 删除, 移动)

    UITableView Cell的插入/删除 核心API Class : UITableView Delegate : UITableViewDataSource, UITableViewDelega ...

  9. UITableView编辑模式

    UITableView有两种模式,普通模式和编辑模式.在编辑模式下可以对cell进行排序.删除.插入等等. 如何进入编辑模式 调用tableView的setEditing(editing: Bool, ...

随机推荐

  1. html drag 拖拽用法和注意事项

    1.拖拽过程中的事件暂时jQuery里还没有,只能通过html DOM 来进行绑定,不然无法获取dataTransfer对象 2.在dragstart .dragover 等事件中可以用 evt.pr ...

  2. spring aop 中获取 request

    使用aop时需要request 和response 使用方法调用时 HttpServletRequest request = ((ServletRequestAttributes)RequestCon ...

  3. php + Bootstrap-v3-Typeahead 自动完成组件的使用

    Bootstrap 中的 Typeahead 组件就是通常所说的自动完成 AutoComplete,类似百度.谷歌等搜索提示:输入关键词出现相应的下拉列表数据. 是Bootstrap-3-Typeah ...

  4. QT开发实战精解

    无法打开包括文件<QApplication> No such file or directory  这一问题 解决办法,使用QApplication时必须在项目pro文件中添加一句 QT ...

  5. Prince2和PMP的区别,大多数人都没有搞清楚!

    [涨姿势]Prince2和PMP的区别,大多数人都没搞清楚!   项目管理领域有2个流行的知识体系:☑ 一个是美国项目管理协会(PMI)开发的"项目管理知识体系(PMBOK,Project ...

  6. 【转】Repository has not been enabled to accept revision propchanges

    转载地址:http://lg-zhou.blog.163.com/blog/static/178068920111179341041/ 使用SVN提交版本信息时,注释内容写的不全.通过右键Tortoi ...

  7. (转) cocos2dx 显示中文

    cocos2dx 中文显示 分类: cocos2dx 2014-01-18 10:31 253人阅读 评论(0) 收藏 举报 目录(?)[+] 总结一句话,就是UTF-8编码. 1. 中文FNT字体文 ...

  8. python(三)set集合

    set集合的特点是无序.不重复序列 创建集合: 1 2 3 4 5 6 7 8 9 10 11 12 13 a.s1 = {11,22} b.s2 = set() c.s3 = set([11,22, ...

  9. Exchange Server 2013就地存档

    9.1就地存档 就地存档有助于重新获得对组织邮件数据的控制,而无需个人存储 (.pst) 文件,并且允许用户在可通过 Microsoft Outlook 2010及更高版本和 Microsoft Of ...

  10. How To Use FETCH_RECORDS In Oracle Forms

    When called from an On-Fetch trigger, initiates the default Form Builder processing for fetching rec ...