一、tableView的编辑
tableView 编辑的步骤:
 
 *  1. 让tableView成为可编辑状态
 
    编辑按钮触发方法
 
 *  -1. 激活编辑状态
- (void)rightButton:(UIBarButtonItem *)rightButton
{
   // 开启UITableView 编辑状态
    // self.tableView.editing 默认是 NO 的
    [self.tableView setEditing:!self.tableView.editing animated:YES];
   
   // 更改按钮的标题
    if (_tableView.editing) {
        rightButton.title = @"完成";
    } else {
        rightButton.title = @"编辑";
    }
}
 *  2. 返回(指定)可以被编辑的分区的行  (默认是YES)
  // 如果不写的话 默认是YES
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    return YES;
   
}
 *  3. 返回(指定)可以被编辑的分区的行的样式  添加 Or 删除
  // 返回哪个分区的哪行的 编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 先判断分区
    if (indexPath.section == 0) {
        // 再判断数据
        if ([self.firstDataArray[indexPath.row] isEqualToString:@"添加"]) {
            return UITableViewCellEditingStyleInsert;
        }
    } else if (indexPath.section == 1) {
        // 再判断数据
        if ([self.secondDataArray[indexPath.row] isEqualToString:@"添加"]) {
            return UITableViewCellEditingStyleInsert;
        }
    }
    return UITableViewCellEditingStyleDelete;
}
 *  4. 按照编辑的样式 来提交编辑的结果,完成不同的操作
 
        -1-  √ 更新数据源数据
        -2-  √ 刷新界面
int i = 4, j = 4;
// 4. 按照编辑的样式 提交哪个分区哪行的结果 完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    NSString *str = [NSString stringWithFormat:@"%d", i];
    NSString *str1 = [NSString stringWithFormat:@"%d", j];

// 更新数据
   
    // 刷新界面
   
    // 先判断分区
    if (indexPath.section == 0) {
        // 第一分区
            // 再判断编辑样式
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // 删除
                // 利用indexPath.row 删除数组中相对应的数据
            [self.firstDataArray removeObjectAtIndex:indexPath.row];
            // 刷新界面
                // 下面方法 用在删除数据的时候刷新界面
                // 需要一个数组 数组中是删除的索引  这个数组可以是多行的索引
            // 下面的刷新方法是对行进行操作的
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
        } else {
            // 添加
           
         //  [self.firstDataArray insertObject:str atIndex:[self.firstDataArray count] - 1];
            [self.firstDataArray insertObject:str atIndex:indexPath.row];
            [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
            i++;
        }
    } else {
        // 第二分区
        // 再判断编辑样式
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // 删除
            [self.secondDataArray removeObjectAtIndex:indexPath.row];
            // 刷新界面
            // 下面方法 用在删除数据的时候刷新界面
            // 需要一个数组 数组中是删除的索引  这个数组可以是多行的索引
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];

} else {
            // 添加
            [self.secondDataArray insertObject:str1 atIndex:indexPath.row];
            [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationBottom)];
            j++;
        }
    }

}
二、tableView的移动
tableView 移动的步骤:
 
 *  1. 让tableView成为可编辑状态
  /**编辑按钮触发方法
 
 *  1. 激活编辑状态
 *
 *
 */
- (void)rightButton:(UIBarButtonItem *)rightButton
{
   // 开启UITableView 编辑状态
    // self.tableView.editing 默认是 NO 的
    [self.tableView setEditing:!self.tableView.editing animated:YES];
   
   // 更改按钮的标题
    if (_tableView.editing) {
        rightButton.title = @"完成";
    } else {
        rightButton.title = @"编辑";
    }
}
 *  2. 返回(指定)可以被移动的分区的行  (默认的是YES)
  - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
 *  3. 完成移动后  -1- 更新数据  -2- 刷新界面
// 移动完成

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
// sourceIndexPath 来源的索引  (拿起来cell 的位置)
// destinationIndexPath  目的地/终点的索引  (将来要放下cell  的位置)
{
    // 分两种情况 1. 同section 的移动(同区移动), 不同section 之间的 移动(跨区移动)
    if (sourceIndexPath.section == destinationIndexPath.section) {
        // 同区
        if (sourceIndexPath.section == 0) {
            // 操作第一分区数组
            // 先保存一下来源索引处的数据
            NSString *str = self.firstDataArray[sourceIndexPath.row];
            // 再从数组中 按来源索引删除该数据
            [self.firstDataArray removeObjectAtIndex:sourceIndexPath.row];
            // 最后再把保存的数据 插入到目的地的索引处
            [self.firstDataArray insertObject:str atIndex:destinationIndexPath.row];
           
            // 刷新页面
            [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
           
        } else{
            // 操作第二分区数组
            NSString *str = self.secondDataArray[sourceIndexPath.row];
            [self.secondDataArray removeObjectAtIndex:sourceIndexPath.row];
            [self.secondDataArray insertObject:str atIndex:destinationIndexPath.row];
            [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
        }
    } else {
        // 跨区

     }
4.重要的.
***** iOS 中是不允许跨区移动的
// 系统有一个自带的方法限制跨区移动

// 只要拖动 就会触发这个方法
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    // sourceIndexPath 来源的索引  (拿起来cell 的位置)
    // proposedDestinationIndexPath  推荐/建议的 目的地/终点的索引  (将来要放下cell  的位置)
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    } else {
        return sourceIndexPath;
    }

}
三、UITableViewController
 *  UITableViewController
        1. 自带了一个 self.tableView 根屏幕一样大
 
        2. 代理协议 数据源 已经给写好了
       
        3. 实现了一部分 代理方法

UI一揽子计划 10 (UITableView 中cell 的编辑, 增加, 删除、UITableViewController 、)相关推荐

  1. iOS开发UI篇-在UItableview中实现加载更多功能

    iOS开发UI篇-在UItableview中实现加载更多功能 一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据. 二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时 ...

  2. UI一揽子计划 21 (UICollectionView、SDWebImage第三方类库加载图片的使用、集合视图的布局UICollectionViewFlowLayout 、自定义Cell、布局协议

    Pro : SDWebImage第三方类库加载图片的使用: 1.在MRC 环境下 使用ARC 的类库: -fobjc-arc   (Build Phases  --> Compile Sourc ...

  3. UI一揽子计划 9 (UITableView 、UITableView 、重用机制)

    一. UITableView UITableView继承自UIScrollView,所以可以滚动 表视图的每⼀一条数据都是显示在UITableViewCell对象中 表视图可以分区显⽰示数据,每个分区 ...

  4. Repeater控件中如何做编辑和删除功能

    做asp.net有六年了,Repeater控件使用了无数次,但每次都是只做显示. 今天这个需要编辑和删除功能,google了一下.Repeater需要加OnItemDataBound事件. OnIte ...

  5. UI一揽子计划 11 (自定义UITableViewCell、Cell 的自适应高度)

    一. 自定义UITableViewCell 在日常的编程中,系统提供的几种Cell 样式 往往不能满足我们的需求.所以需要我们给它进行自定义样式. 自定义Cell 就是创建一个UITableViewC ...

  6. UI一揽子计划 1 (UIView UILabel)

    第一天 1.UIView ·建立一个空模板后,首先将ARC模式关闭,因为iOS采用MRC模式,即手动管理内存. ·重写dealloc方法. - (void)dealloc { [_window rel ...

  7. UITableView中cell的圆角(第一个和最后一个)

    #import <UIKit/UIKit.h> @interface SemicircleView : UIView /**  *  传入数值(改变圆角位置)  *  *  @param  ...

  8. UI一揽子计划 14 (拖控件 、)

    - (void)viewDidLoad {     [super viewDidLoad];         // 注册cell         // 如果使用Xib 文件加载cell 的话 一定要注 ...

  9. UI一揽子计划 2 (UITextField  UIButton)

    UItextField 一.创建一个UITextField时候默认就弹出键盘     1 对齐方式     textField.textAlignment = NSTextAlignmentCente ...

最新文章

  1. 有人能用外行人解释什么是JSONP吗? [重复]
  2. 向碳基芯片更进一步:台积电、斯坦福等联手开发碳纳米管晶体管新工艺,性能逼近硅元件...
  3. tensorflow r1.5 版本差异调研
  4. Tomcat的web项目部署方式
  5. 工作45:注意公用方法 别混入
  6. 剑指offer:12-17记录
  7. 【codevs2952】 细胞分裂2,快速幂模版
  8. 产品经理学习---人性七宗罪:打造完美产品的金钥匙
  9. CSS之position解释
  10. Java思维导图(3)
  11. VB2010实例(2) _滚动字幕
  12. UVA 1212 Duopoly
  13. 【ML学习笔记】2:机器学习中的数学基础2(琴生不等式,概率公式,统计量)
  14. Unity iOS包打出的app名称空格丢失
  15. 极客日报:苹果或推出粉色款iPhone 13;拼多多再超阿里
  16. 百度云视频 在线倍速播放
  17. 网站常见的攻击与防护
  18. 阿呆喵广告过滤 v1.9.0.1 官网版
  19. php刷新热销商品,ecshop首页如何调用指定分类下的推荐热销最新商品
  20. 【Algorithm】种子填充算法

热门文章

  1. 自然语言处理NLP星空智能对话机器人系列:业界动态信息分享 京东零售数据智能探索实践沙龙
  2. Google挖坑后人埋-ViewBinding(上)
  3. P4论文粗读笔记(一)
  4. PID控制算法学习与Matlab仿真
  5. Kubernetes 线下培训班
  6. c语言12之编程设计一个简单的计算器程序,要求根据用户从键盘输入的表达式:操作数1 运算符op 操作数2 计算表达式的值,指定的运算符为加减乘除。
  7. 电脑常用的快捷键Ctrl篇
  8. 学习linux第三步:给英文版的ubuntu18.04安装中文输入法
  9. This.invoke和this.begininvoke的区别?
  10. 【答学员问】如何提问问题