一、UITableView编辑

  • UITableView编辑(删除、添加)步骤:
  1. 让TableView处于编辑状态。
  2. 协议设定:1)确定Cell是否处于编辑状态;2)设定cell的编辑样式(删除、添加);3)编辑状态进行提交。
  3. 注意:编辑结束后,由于numberOfRowSInSection这个协议只在tableView添加到父视图的时候走一次,而且table上的数据都是由数组提供,因此,需要先将数组中的元素删除,然后让table的协议重新走一遍进行重新赋值。即:先修改数据源,再刷新table。
  4. 代码示例:(因为要在两个按钮上添加不同的编辑样式,所以先在延展里声明了编辑样式属性)
    @interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
    // 声明属性编辑样式
    @property (nonatomic, assign) UITableViewCellEditingStyle style;
    @end
#pragma mark UITableView 编辑------
  // 实现左按钮点击事件
  - (void)leftClick
  {
      // 返回编辑样式要写在前面
      self.style = UITableViewCellEditingStyleInsert;
      [self.rootView.tableView setEditing:!self.rootView.tableView.editing animated:YES];

  }
  // 实现右按钮点击事件
  // 第一步:让TableView处于编辑状态
  - (void)rightClick
  {

      self.style = UITableViewCellEditingStyleDelete;
      [self.rootView.tableView setEditing:!self.rootView.tableView.editing animated:YES];
  }

  // 第二步:指定哪些cell可以被编辑
  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  {
      ) {
          return YES;
      }else {
        return NO;
      }
  }

  // 第三步:设置编辑样式
  - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  {

      return self.style;
  }

  // 第四步:完成编辑,提交编辑状态
  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  {
      // 判断编辑样式
      ) {
          // 1.删除数据(顺序不能变)
          [self.allDataArray[indexPath.section] removeObjectAtIndex:indexPath.row];

          // 2.更新UI
          // 更新一行
          [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
          // 全部更新
          //    [tableView reloadData];
      }) {
          // 1.添加数据到数组中
          [self.allDataArray[indexPath.section] insertObject:];
          // 2.更新UI
          // 创建新一行的NSIndexPath对象,添加到下一行
          NSIndexPath *newIndexpath = [NSIndexPath indexPathForRow:indexPath.row +  inSection:indexPath.section];
          [tableView insertRowsAtIndexPaths:@[newIndexpath] withRowAnimation:UITableViewRowAnimationRight];
      }
  }
  • UITableView移动操作步骤:
#pragma mark UITableView 移动
    // 实现右按钮点击事件
    - (void)rightClick
    {
        // 处于编辑状态
        [self.rootView.tableView setEditing:!self.rootView.tableView.editing animated:YES];
    }
    //实现协议:告诉tableView是否能移动
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:    (NSIndexPath *)indexPath
    {
        return YES;
    }
    // 移动
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    {
        // 1.获取需要修改的数据
        NSString *sourceName = [self.allDataArray[sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];
        // 2.先将数据从当前位置移除
        [self.allDataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
        // 3.将数据插入到对应位置
        [self.allDataArray[destinationIndexPath.section] insertObject:sourceName atIndex:destinationIndexPath.row];

    }
#pragma mark 防止随意移动
    - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
    {
        // 如果是本组  移动
        if (sourceIndexPath.section == proposedDestinationIndexPath.section) {

            return proposedDestinationIndexPath;

        }
        // 如果不是 不能移动
        else {
            return sourceIndexPath;
        }
    }

二、UITableVIewController

  • UITableViewController概述:UITableViewController是继承于UIViewController中的一个类,简单的说UITableViewController是一个自带tableView的视图控制器。
    @interface UITableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
  • UITableViewController的特点:
  1. UITableViewController自带tableView。
  2. self.view不是UIView而是UITableView。
  3. datasource和delegate默认都是self。
  4. 因为继承于UIViewController,所以UITableViewController不用遵守协议就可以使用协议方法。
  5. 代码示例(一个新创建的UITableViewController):
    #import "TableViewController.h"

    @interface TableViewController ()

    @end

    @implementation TableViewController

    - (void)viewDidLoad {
        [super viewDidLoad];

        // Uncomment the following line to preserve selection between presentations.
        // self.clearsSelectionOnViewWillAppear = NO;

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

  #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  #warning Incomplete implementation, return the number of sections
        ;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  #warning Incomplete implementation, return the number of rows
        ;
    }

    /*
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];

        // Configure the cell...

        return cell;
    }
    */

    /*
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the specified item to be editable.
        return YES;
    }
    */

    /*
    // Override to support editing the table view.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // Delete the row from the data source
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        } else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }
    */

    /*
    // Override to support rearranging the table view.
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the item to be re-orderable.
        return YES;
    }
    */

    /*
#pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    @end

iOS学习之UITableView编辑的更多相关文章

  1. iOS学习之UITableView中Cell的操作

    接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记.移动.删除.插入. 为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http ...

  2. iOS学习之UITableView

    一.UITableView的概念 UITabelView继承于UIScrollView,可以滚动. @interface UITableView : UIScrollView <NSCoding ...

  3. ios学习笔记 UITableView(纯代码) (二)

    头文件 --------------------------------------------- #import <UIKit/UIKit.h> /** UITableViewDataS ...

  4. ios学习笔记 UITableView(纯代码) (一)

    参考 “https://www.cnblogs.com/ai-developers/p/4557487.html” UITableViewCell 有一个代码重用 减少资源的浪费 参考  https: ...

  5. iOS学习30之UITableView编辑

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

  6. iOS学习——tableview中带编辑功能的cell键盘弹出遮挡和收起问题解决

    最近在项目中经常用到UITableView中的cell中带有UITextField或UITextView的情况,然后在这种场景下,当我们点击屏幕较下方的cell进行编辑时,这时候键盘弹出来会出现遮挡待 ...

  7. iOS学习笔记之UITableViewController&UITableView

    iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...

  8. iOS学习笔记(4) — UITableView的 重用机制

    iOS学习笔记(4) — UITableView的 重用机制 UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的 ...

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

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

随机推荐

  1. android反编译工具总结

    序:总结反编译主要的目的在于学习.利用反编译进行相关的汉化或修改,都是不道德的! 大家都知道,将apk文件解压后有两部分文件需要处理,一种是xml文件,另一种一个dex文件(.dex),我们可以从.d ...

  2. MySQL:MySQL和SQL Server的区别

    导读:接下来的网上商城的项目,需要用到MySQL数据库了.这个对于我来说,是一个新接触的东西,按照惯例,在刚开始学习一个东西的时候,先从宏观上去了解它.本篇博客,先介绍SQL Server的基本内容, ...

  3. hql语句关联查询(select new )

    在  new bean()中,对象里要有其构造方法,查询参数必须是构造方法中的参数,get set也要同步 类似于 String hql= "select  new A(a.id, a.na ...

  4. docker学习(二)

    1 镜像 docker使用联合加载技术,一次同时加载多个文件系统,在外面只能看到一个文件系统 docker使用写时复制,每个只读镜像层都是只读的,也永远不会发生变化,建立一个新容器,会构建一个镜像栈, ...

  5. Python之路【第五篇】:面向对象及相关

    Python之路[第五篇]:面向对象及相关   面向对象基础 基础内容介绍详见一下两篇博文: 面向对象初级篇 面向对象进阶篇 其他相关 一.isinstance(obj, cls) 检查是否obj是否 ...

  6. 搭建高性能计算环境(二)、远程登录Linux服务器

    一般操作Linux系统都是通过远程登录使用的,本节介绍几种远程登录Linux.上传下载文件的工具. 1. Secure Shell SSH 简单方便.既能使用命令行登陆也能传文件,免费. 打开SSH ...

  7. jquery中选择ID以什么字符开头的匹配主要用于多个上传控件的时候,id无法使用,而且class不起作用的时候

    $("[id^=remark]")选择ID以remark开头的所有数据进行匹配

  8. C#获取程序所在目录路径

    方法1:Directory.GetCurrentDirectory().这个方法只能在.NET的完整版中使用,NETCF中不支持该功能,调用时会引发异常.获取的是当前目录,并不一定是真正的路径,跟Op ...

  9. Ubuntu开发环境搭建

    linux开发不得不用虚拟机,为了节省系统资源.决定采用Ubuntu Server逐步搭建出具有图形界面的开发环境. ubuntu server 安装英文版 安装选择选generic,不要LVM选项. ...

  10. 一个自定义的C#数据库操作基础类 SqlHelper

    SqlHelper其实是我们自己编写的一个类,使用这个类目的就是让使用者更方便.更安全的对数据库的操作,既是除了在SqlHelper类以外的所有类将不用引用对数据库操作的任何类与语句,无须担心数据库的 ...