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. 有关 C# 命名参数和可选参数

    有关 C# 命名参数和可选参数 #1.命名参数: 所谓“命名参数 ( Named Arguments )”,是指方法中定义了一些“有名字”的参数. 给方法参数命名之后,在调用方法时就可以直接根据参数名 ...

  2. 【MFC】获取文件大小的方法

    [转载]原文地址:http://blog.csdn.net/coderwu/article/details/5652056 MFC 下可以通过 CFileStatus 获取文件大小. ULONGLON ...

  3. 时序js插件cubism使用

    document http://iwantmyreal.name/blog/2012/09/16/visualising-conair-data-with-cubism-dot-js https:// ...

  4. EF ObjectQuery查询及方法

      string esql = "select value c from NorthwindEntities.Customers as c order by c.CustomerID lim ...

  5. 判断一个点是否在多边形区域内--C算法

    /*函数的输入:(1)当前点的坐标p(2)区域顶点数组pt[]:(3)顶点数nCount 输出: 在区域内返回TRUE,否则返回FALSE.  Point类型是一个结构: struct Point { ...

  6. 解决IE下面诡异地使用quickrIE5模式打开页面的有关问题

    解决IE下面诡异地使用quickrIE5模式打开页面的有关问题 <!doctype html public "-//w3c//dtd html 4.01 transitional//e ...

  7. C++,C++编程,Windows编程,MFC

    编程 我们日常生活中接触到的电子类产品中的应用都是由编程而来 为什么编程,偷懒 我们通过编程驱使(指挥,命令)的是电信号 为什么上面说编程是偷懒,电的发现,给人们带来了便利,人们在各个方面驱使(换成“ ...

  8. 异常:System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms 这个实现是不是Windows平台FIPS验证的加密算法。解决方法

    遇见这个问题是在使用了MD5加密算法后报错的,可能的原因如下: 1.FIPS不兼容MD5,此时需要修改注册表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\C ...

  9. kubernetes-ingress(十)

    ingress https://kubernetes.io/docs/concepts/services-networking/ingress/ pod与ingress的关系 •通过label-sel ...

  10. H1ctf-Vote

    用来练习IO_FILE利用 glibc-2.23 # coding:utf-8 from pwn import * from FILE import * context.arch = 'amd64' ...