IOS开发学习笔记032-UITableView 的编辑模式
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 的编辑模式的更多相关文章
- iOS开发学习笔记:基础篇
iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...
- ios开发学习笔记(1)
objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = ...
- IOS开发学习笔记042-UITableView总结2
一.自定义非等高的cell 如常见的微博界面,有的微博只有文字,有的有文字和图片.这些微博的高度不固定需要重新计算. 这里简单说一下几种方法.前面的步骤和设置等高的cell一样.现在来 ...
- iOS开发学习笔记
1 常用的第三方工具 1.1 iPhone Simulator 测试程序需要模拟器iPhone Simulator 1.2 设计界面需要Interface Builder,Interface Buil ...
- ios开发学习笔记(这里一定有你想要的东西,全部免费)
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- IOS开发学习笔记026-UITableView的使用
UITableView的简单使用过程 简单介绍 两种样式 UITableViewStylePlain UITableViewStyleGrouped 数据显示需要设置数据源,数据源是符合遵守协议 &l ...
- IOS开发学习笔记017-第一个IOS应用
第一个IOS应用程序,就从最简单的开始吧. 1.先了解一下开发环境,Xcode的相关组成 2.还有模拟器 3.运行与停止按钮 4.新建一个工程 5.看看main函数里都有啥 6.现在来添加一个控件 1 ...
- (ios开发学习笔记一)ios项目文件结构
转自:http://www.cnblogs.com/macroxu-1982/archive/2012/07/31/2616389.html 下面是单个窗体项目例子,我们从这个项目开始,说明ios项目 ...
- IOS开发学习笔记043-QQ聊天界面实现
QQ聊天界面实现 效果如下: 实现过程: 1.首先实现基本界面 头像使用 UIImageView : 文字消息使用 UIButton 标签使用 UILable :水平居中 所有元素在一个cell中,在 ...
随机推荐
- [转]latex输入数学符号速查
基本运算 乘法 x×y x \times y 乘方 23x 2^{3x} 平方根 x+y−−−−−√ \sqrt {x + y} 除法 x÷y x \div y 分数 xy \frac{x}{y} 异 ...
- MySQL使用一张表的一列更新另一张表的一列
使用MySQL中,在一张表etl_table_field_info上新增了一个字段tgt_table_en_name,该字段的值想从表etl_table_property_info的tgt_table ...
- springMvc-视图模型封装及注解参数
1.视图模型封装,ModelAndView可以向页面返回视图的同时吧模型也传入页面 2.注解参数,springMvc很好的地方在于简单,高效,@RequestParam注解能非常好的取得页面参数 代码 ...
- 【BZOJ3460】Jc的宿舍(树上莫队+树状数组)
点此看题面 大致题意: 一棵树,每个节点有一个人,他打水需要\(T_i\)的时间,每次询问两点之间所有人去打水的最小等待时间. 伪·强制在线 这题看似强制在线,但实际上,\(pre\ mod\ 2\) ...
- DOS&8086微处理器
DOS DOS环境,需要安装dosemu来模拟DOS环境(Ubuntu的应用商店就有),为了编写汇编,还需要DEBUG.MASM.LINK等汇编语言开发工具.如果你嫌麻烦,推荐使用实验楼已搭好的免费的 ...
- 使用RichTextBox控件保存文件
实习效果: 知识运用: RichTextBox控件的SaveFile方法 SaveFileDialog对象的ShowDialog方法 实现代码: private void 打开RTF文件ToolStr ...
- os.walk 模块
os.walk()可以得到一个三元tupple(dirpath, dirnames, filenames),其中第一个为起始路径,第二个为起始路径下的文件夹,第三个是起始路径下的文件. 其中dirpa ...
- 基础I/O
基础IO: c库文件IO操作接口:(详细查看c语言中的文件操作函数总结:https://www.cnblogs.com/cuckoo-/p/10560640.html) fopen 打开文件 fclo ...
- css代码
#footr { background: #3e434a } #header #blogTitle { background: url("http://images.cnblogs.com/ ...
- Java - 通过私有构造方法获取实例