UITableView编辑模式

UITableView有两种模式,普通模式和编辑模式。在编辑模式下可以对cell进行排序、删除、插入等等。

  1. 如何进入编辑模式
    调用tableView的setEditing(editing: Bool, animated: Bool)方法。

    • 进入编辑模式以后发生了什么

      • 向每个cell发送setEditing:animated:方法

      • 进入编辑模式以后cell的变化


        普通模式下cell的contentview的bounds就是cell的bounds.
        编辑模式下,cell有三部分组成,左边的Editing control,中间的contentview,右边的reordering control。这时contentview的位置和大小都发生了变化。
        所以一般情况下,把需要展示的东西加到contentview上,而不是cell上。文档是也有说明。

        The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode

  • 删除模式

    • 指定模式为删除模式

      func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
      return .Delete
      }
      
    • 定制删除提示语
      func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
      return "我删除啦"
      }
      

      这时,cell右侧又多出来一个部分,叫做UITableViewCellDeleteConfirmtionView,不同的删除提示语,长度不一样。

  • 插入模式
    • 将style设为insert就好

      func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
      return .Insert
      }
      
    • 展示右边的排序按钮
      func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
      //do something
      }
      

      实现这个方法就好,即使什么都不做

  • 展示多选按钮
        tableView?.allowsMultipleSelectionDuringEditing = true
        tableView?.tintColor = UIColor.blackColor()
    

  • swipe to delete
    • 实现相应的代理方法

      func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
      }
      

      这个方法里不能调用setEditing(_:animated:)方法

    • tableView(_:editingStyleForRowAt:)方法必须返回delete
时间: 2024-08-26 13:41:03

UITableView编辑模式的相关文章

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

**********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewController () <UITableViewDataSource, UITableViewDelegate> /** 数据列表 */ @property (nonatomic, strong) NSMutableArray *dataList; @property (nonatomic, strong

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

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.dataArray = [NSMutableArray arrayWithArray: @[@"1",@"2",@"3",@"4",@"5",@"6"

第九章 UITableView编辑模式笔记

一,tableview自带编辑模式,可以添加.删除.移动item 二,可以添加section或者table的header和footer 三,使用interface Builder创建header的layout 四,UITableView显示header前,会向它的controller发送headerVIew消息 - (UIView *)headerView { // If you have not loaded the headerView yet... if (!_headerView) {

tableView 编辑模式

UITableView 编辑模式详解 UITableView 的相关编辑操作非常全,今天我们来做一个总结.跟编辑相关的属性和接口有如下,我们一个一个分析,我们先认真阅读一下相关头文件,我根据意思大概翻译了一下注释. 属性方法 @property (nonatomic, getter=isEditing) BOOL editing; // 默认状态是非编辑状态,如果不调用下面接口直接设置,是没有动画的 - (void)setEditing:(BOOL)editing animated:(BOOL)

【UIKit】UITableView.07 编辑模式

[1]拖动好界面 [2]设置协议,数据源 [3]代码 1.声明可变数组,用来存放所有数据对象 @interface ViewController () @property(nonatomic,strong)NSMutableArray *mydata; @end 2.初始化数据[创建30个对象数据] - (void)viewDidLoad { [super viewDidLoad]; self.mydata=[NSMutableArray array]; for(int i =0;i<30;i+

UITableView的编辑模式

UITableView可以分普通模式和Editing模式两种,这里我们着重讨论Editing模式,Editing模式中又分三种操作:Insert.Delete. Reallocted.Insert和Delete针对数据源内容的修改,而Reallocated是针对数据源位置的修改.下面分别讨论. 一.Insert Or Delete 当UITableView接收到 setEditing:animated:时,它会将同样的消息转发给每一个可见行,大致会经历如下步骤,引用至官方: The table

ios之UITableViewController(二) tableView的编辑模式

tableView的编辑模式 表视图可以进入编辑模式,当进入编辑模式就可以进行删除.插入.移动单元等操作 效果图: 让表视图进入编辑模式,进入编辑模式的方法有两种,一种是使用导航栏的edit 按钮,另一种是设置tableView的editing属性进入编辑模式. 最后通过实现UITableViewDataSource协议的方法实现单元格的删除.插入和移动 1,在viewDidLoad方法里面指定导航栏的右按钮为edit按钮 self.navigationItem.rightBarButtonIt

IOS开发——UI进阶篇(四)全局刷新,局部刷新,左滑操作,左滑出现更多按钮,进入编辑模式,批量删除,自定义批量删除

首先创建项目,在storyboard如下布局控件,设置好约束 然后创建cell模型类XMGWineCell数据模型类XMGWine创建UITableView,设置数据源协议,实现数据源方法懒加载数据这些在前面已经做过很多次了,代码就不一一写了 一.全局刷新 1.添加单组数据并全局刷新 - (IBAction)add { // 添加模型数据 XMGWine *wine = [[XMGWine alloc] init]; wine.money = @"20.5"; wine.name =

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

UITableView Cell的插入/删除 核心API Class : UITableView Delegate : UITableViewDataSource, UITableViewDelegate 涉及的API:(API的官方详细注释详见本章结尾) /** TableView 进入或退出编辑状态(TableView 方法). */ - (void)setEditing:(BOOL)editing animated:(BOOL)animate /** 确定哪些行的cell可以编辑 (UIT