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中,在 ...
随机推荐
- Spring MVC的高级配置
1.文件上传配置 文件上传是项目中常用的一个功能,Spring MVC通过配置一个MultipartResolver来上传文件. 在Spring的控制器中,通过MultipartFile file 来 ...
- python3基础07(进程操作及执行系统级命令等)
#subprocess 创建子进程 连接输入 输出 管道错误,及获取他们的状态,可执行操作系统级的命令# subprocess.run(args, *, stdin=None, input=None, ...
- html5 app开发实例 Ajax跨域访问C# webservices服务
通过几天的研究效果,如果在vs2010工具上通过webservice还是比较简单的,毕竟是一个项目. 如果您想通过HTML5 做出来的移动APP去访问c#做出来的webservice,那么就没那么简单 ...
- LeetCode Longest Substring Without Repeating Characters 最长不重复子串
题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...
- 流媒体 8——因特网 tcp/ip
1 因特网 1.1 因特网的结构 组成因特网的子网之间在物理上的相互连接都是通过网关设备实现的.通过网关设备互相连接在一起的不同的网络通常称为子网 (subnetwork),因为它们是大网络之中的网络 ...
- 【BZOJ1040】[ZJOI2008] 骑士(基环外向树DP)
点此看题面 大致题意: 给你一片基环外向树森林,如果选定了一个点,就不能选择与其相邻的节点.求选中点的最大权值和. 树形\(DP\) 此题应该是 树形\(DP\) 的一个升级版:基环外向树\(DP\) ...
- 【BZOJ1059】[ZJOI2007] 矩阵游戏(匈牙利算法)
点此看题面 大致题意: 有一个\(N*N\)的\(01\)矩阵,可以任意交换若干行和若干列,问是否有方案使得左上角到右下角的连线上全是\(1\). 题意转换 首先,让我们来对题意进行一波转化. 如果我 ...
- python_47_Python2中字符编码与转码
#python3默认是Unicode,Unicode是万国码,不管中文字符还是英文,所有的每个字符都占2个字节空间,16位 #python2默认是ascii码 #ascii码不能存中文,一个英文只能占 ...
- ssh: connect to host localhost port 22: Connection refused
1.hadoop安装好之后,执行ssh localhost无法执行, 提示“ssh: connect to host localhost port 22: Connection refused”. 2 ...
- tomcat服务器用Servlet类查找磁盘文件上的Json信息,如果匹配则在浏览器上显示出该条内容的全部信息
package com.swift; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOE ...