UITableView 的三种编辑模式

1、删除

2、排序

3、添加

进入编辑模式,需要设置一个参数

 - (IBAction)remove:(UIBarButtonItem *)sender
{
NSLog(@"removed");
// 进入编辑模式
BOOL removed = !self.tableView.isEditing; //获取当前状态进行取反
[self.tableView setEditing:removed animated:YES]; //设置编辑模式,并设置动画
}

1、实现界面

界面组成为两个lable标签,新建一个模型类Person保存数据,直接使用默认UITableViewCell默认的cell,设置textLable控件和detailLable控件,然后设置显示样式为 UITableViewCellStyleValue1,

两个标签并排显示。至于怎么初始化显示,我这里就不说了,可以看以往的文章。

2、删除

响应删除按钮需要实现一个方法 commitEditingStyle

 // 进入编辑模式,点击删除会调用这个方法,实现这个方法就可以出现向左滑动出现删除按钮
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 如果不是删除模式就退出
if (editingStyle != UITableViewCellEditingStyleDelete) {
return;
}
// 1、获取cell
Person *p = _data[indexPath.row];
// 2、删除cell
[_data removeObject:p];
// 3、更新cell
//reloadRowsAtIndexPaths 使用前提就是模型数据没有改变
//[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 模型对象数据内容改变,可以用这个
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 模型对象个数改变,用这个方法 }

3、排序

 // 编辑模式下得排序功能
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 1、获得拖动对象
Person *p = _data[sourceIndexPath.row];
// 2、删除拖动对象
[_data removeObject:p];
// 3、插入拖动对象到最新位置
[_data insertObject:p atIndex:destinationIndexPath.row];

4、添加

添加的话,可能有点麻烦,主要是把单击添加按钮的这个消息传递当方法 editingStyleForRowAtIndexPath

这里使用设置addBtn按钮的tag来标识按钮是否按下,默认是0,按下设置为2。

按钮按下时设置tag

 - (IBAction)add:(UIBarButtonItem *)sender
{
// 进入编辑模式
//isAdd = YES;
_addBtn.tag = ; // 设置按钮tag为2
BOOL added = !self.tableView.isEditing;
[self.tableView setEditing:added animated:YES]; // 设置为编辑模式
}

设置显示样式

 // 设置每一行的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(_addBtn.tag == ) // 添加按钮是否按下
return UITableViewCellEditingStyleInsert; // 添加
else if(_addBtn.tag == )
return UITableViewCellEditingStyleDelete; // 删除
else
return UITableViewCellEditingStyleNone; // 默认
}

添加cell

 // 进入编辑模式,点击删除会调用这个方法,实现这个方法就可以出现向左滑动出现删除按钮
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"editingStyle");
// 如果是删除模式
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// 1、获取cell
Person *p = _data[indexPath.row];
// 2、删除cell
[_data removeObject:p];
// 3、更新cell
//reloadRowsAtIndexPaths 使用前提就是模型数据没有改变
//[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 模型对象数据内容改变,可以用这个
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 模型对象个数改变,用这个方法 }
// 如果是添加模式
else if(editingStyle == UITableViewCellEditingStyleInsert)
{
NSLog(@"insert");
// 1、获取cell
Person *p = [Person personWithName:@"personAdd" andPhone:@""];
// 2、插入cell
[_data insertObject:p atIndex:indexPath.row + ];
// 3、更新cell
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView reloadData];
} }

源代码

 //
// ViewController.m
// UITableView-编辑模式
//
// Created by Christian on 15/5/26.
// Copyright (c) 2015年 slq. All rights reserved.
// #import "ViewController.h"
#import "Person.h" @interface ViewController () <UITableViewDataSource> {
NSMutableArray *_data;
}
@end @implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 初始化
_data = [NSMutableArray array];
for (int i = ; i < ; i ++)
{
Person *p = [[Person alloc] init];
p.name = [NSString stringWithFormat:@"person-%d",i];
p.phone = [NSString stringWithFormat:@"%d2389823",i];
[_data addObject:p];
}
_addBtn.tag = ;
} - (IBAction)remove:(UIBarButtonItem *)sender
{
// 进入编辑模式
_addBtn.tag = ; // 设置添加按钮tag为0
BOOL removed = !self.tableView.isEditing;
[self.tableView setEditing:removed animated:YES]; } - (IBAction)add:(UIBarButtonItem *)sender
{
// 进入编辑模式
_addBtn.tag = ; // 设置按钮tag为2
BOOL added = !self.tableView.isEditing;
[self.tableView setEditing:added animated:YES];
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _data.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 从缓存池中读取cell
static NSString *ID = @"Person";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 如果缓存池中没有,就新建一个
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
}
// 设置cell数据
Person *p = _data[indexPath.row];
cell.textLabel.text = p.name;
cell.detailTextLabel.text = p.phone;
return cell;
} // 进入编辑模式,点击删除会调用这个方法,实现这个方法就可以出现向左滑动出现删除按钮
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"editingStyle");
// 如果是删除模式
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// 1、获取cell
Person *p = _data[indexPath.row];
// 2、删除cell
[_data removeObject:p];
// 3、更新cell
//reloadRowsAtIndexPaths 使用前提就是模型数据没有改变
//[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 模型对象数据内容改变,可以用这个
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 模型对象个数改变,用这个方法 }
// 如果是添加模式
else if(editingStyle == UITableViewCellEditingStyleInsert)
{
NSLog(@"insert");
// 1、获取cell
Person *p = [Person personWithName:@"personAdd" andPhone:@""];
// 2、插入cell
[_data insertObject:p atIndex:indexPath.row + ];
// 3、更新cell
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView reloadData];
} } // 编辑模式下得排序功能
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 1、获得拖动对象
Person *p = _data[sourceIndexPath.row];
// 2、删除拖动对象
[_data removeObject:p];
// 3、插入拖动对象到最新位置
[_data insertObject:p atIndex:destinationIndexPath.row];
}
// 设置每一行的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(_addBtn.tag == ) // 添加按钮是否按下
return UITableViewCellEditingStyleInsert; // 添加
else if(_addBtn.tag == )
return UITableViewCellEditingStyleDelete; // 删除
else
return UITableViewCellEditingStyleNone; // 默认
} @end

源代码参考: http://pan.baidu.com/s/1rZgGu

IOS开发学习笔记032-UITableView 的编辑模式的更多相关文章

  1. iOS开发学习笔记:基础篇

    iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...

  2. ios开发学习笔记(1)

    objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = ...

  3. IOS开发学习笔记042-UITableView总结2

    一.自定义非等高的cell         如常见的微博界面,有的微博只有文字,有的有文字和图片.这些微博的高度不固定需要重新计算. 这里简单说一下几种方法.前面的步骤和设置等高的cell一样.现在来 ...

  4. iOS开发学习笔记

    1 常用的第三方工具 1.1 iPhone Simulator 测试程序需要模拟器iPhone Simulator 1.2 设计界面需要Interface Builder,Interface Buil ...

  5. ios开发学习笔记(这里一定有你想要的东西,全部免费)

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...

  6. IOS开发学习笔记026-UITableView的使用

    UITableView的简单使用过程 简单介绍 两种样式 UITableViewStylePlain UITableViewStyleGrouped 数据显示需要设置数据源,数据源是符合遵守协议 &l ...

  7. IOS开发学习笔记017-第一个IOS应用

    第一个IOS应用程序,就从最简单的开始吧. 1.先了解一下开发环境,Xcode的相关组成 2.还有模拟器 3.运行与停止按钮 4.新建一个工程 5.看看main函数里都有啥 6.现在来添加一个控件 1 ...

  8. (ios开发学习笔记一)ios项目文件结构

    转自:http://www.cnblogs.com/macroxu-1982/archive/2012/07/31/2616389.html 下面是单个窗体项目例子,我们从这个项目开始,说明ios项目 ...

  9. IOS开发学习笔记043-QQ聊天界面实现

    QQ聊天界面实现 效果如下: 实现过程: 1.首先实现基本界面 头像使用 UIImageView : 文字消息使用 UIButton 标签使用 UILable :水平居中 所有元素在一个cell中,在 ...

随机推荐

  1. python property用法

    参考 http://openhome.cc/Gossip/Python/Property.html http://pyiner.com/2014/03/09/Python-property.html ...

  2. 利用临时表实现CTE递归查询

    一.CTE递归查询简介 --CTE递归查询终止条件在TSQL脚本中,也能实现递归查询,SQL Server提供CTE(Common Table Expression),只需要编写少量的代码,就能实现递 ...

  3. HDU 3351 Seinfeld 宋飞正传(水)

    题意: 给出一个串,串内只有大括号,问经过几次改变可使全部括号合法?改变指的是可以将某一方向的括号变成另一方向. 思路: 利用栈的特点,若出现成对的合法括号,直接删掉,留下那些不合法的成为一串.既然不 ...

  4. YII2 定义页面提示

    控制器里面这样写: 单条消息: 键值是规定好的,不要去自定义哦! \Yii::$app->getSession()->setFlash('error', 'This is the mess ...

  5. PHP实现文件上传和下载(单文件上传、多文件上传、多个单文件上传)(面向对象、面向过程)

    今天我们来学习用PHP进行文件的上传和下载,并且用面向过程和面向对象的方式对文件上传进行一个限制 一.简单的上传测试 1.客户端:upload.php 2.后端:doAction.php 结果: 二. ...

  6. pta 编程题8 Tree Traversals Again

    其它pta数据结构编程题请参见:pta 这次的作业考察的是树的遍历. 题目的输入通过栈的pop给出了树的中序遍历的顺序.根据push和pop的顺序构造树的方法为:定义一个变量father来确定父节点, ...

  7. iOS开发:自定义带下划线文本的UIButton

    Uiunderlinedbutton.h代码 @interface UIUnderlinedButton : UIButton { } + (UIUnderlinedButton *) underli ...

  8. POJ 2184 Cow Exhibition(背包)

    希望Total Smart和Totol Funess都尽量大,两者之间的关系是鱼和熊掌.这种矛盾和背包的容量和价值相似. dp[第i只牛][j = 当前TotS] = 最大的TotF. dp[i][j ...

  9. 简单ssh

    #!/usr/bin/env python #-*- coding:utf-8 -*- # datetime:2019/5/22 14:20 # software: PyCharm #服务端 impo ...

  10. java实现微信扫一扫详解

    java实现微信扫一扫详解 一.微信JS-SDK参数配置及查找 JS安全域名配置(查找:微信公众号里-公众号设置-功能设置页) 注:1.安全域名外网必须可以访问的到  2.域名不能有下划线  3.要将 ...