UI学习笔记---第十天UITableView表视图编辑
UITableView表视图编辑
表视图编辑的使用场景
当我们需要手动添加或者删除某条数据到tableView中的时候,就可以使用tableView编辑.比如微信 扣扣中删除和某人的通话
当我们需要手动调整单元格的顺序时,就可以通过tableView移动,移动单元格到指定位置
代理AppDelegate.m中代码
#import "AppDelegate.h"
#import "RootViewController.h"
@implementation AppDelegate
-(void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; RootViewController *rootVC = [[RootViewController alloc] init];
UINavigationController *ngVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = ngVC; [ngVC release];
[rootVC release]; [self.window makeKeyAndVisible];
return YES;
}
RootViewController.h中建立一个可变数组属性
NSMutableArray *_mArr;
RootViewController.m中初始化和loadView代码
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.navigationItem.title = @"百家讲坛";
self.navigationItem.rightBarButtonItem = self.editButtonItem;//控制器自带的编辑按钮
_mArr = [[NSMutableArray alloc] initWithObjects:@"赵",@"钱",@"孙",@"李",@"周",@"武",@"郑",@"王",@"唐僧",@"孙悟空",@"猪八戒",@"沙僧",@"小白龙",@"二郎神",@"哪吒",@"雷震子", nil];
// Custom initialization
}
return self;
}
-(void)loadView
{ UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(, , , ) style:UITableViewStyleGrouped];
table.dataSource = self;
table.delegate = self;
self.view = table;
[table release]; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_mArr count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [_mArr objectAtIndex:indexPath.row];
// tableView.editing = YES;
//cell右侧属性
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
编辑的步骤
1.让tableView处于编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
//调用父类方法,实现edit和done的变换
[super setEditing:editing animated:animated];
UITableView *tableView = (UITableView *)self.view;
[tableView setEditing:editing animated:animated];
}
2.指定tableView那些行可以编辑
//设置cell的可编辑状态,默认是yes
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.row == 0) {
// return YES;
// }
return YES;
}
3.指定tableView的编辑的样式(添加.删除)
//delegate中得方法
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row ==) {
return UITableViewCellEditingStyleInsert;//添加
}
return UITableViewCellEditingStyleDelete;//删除
}
4.编辑完成(先操作数据源,后修改UI)
//点击加号或者delete时触发的事件
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
//删除数据 (写在删除cell前面 或者写一个[tableView beginUpdates]放前面一个[tableView endUpdates]放后面)
[_mArr removeObjectAtIndex:indexPath.row];
//删除cell
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates];
NSLog(@"删除");
}else{
[tableView beginUpdates];
[_mArr insertObject:@"hello" atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates]; NSLog(@"添加");
}
}
表视图的移动
移动的步骤
1.让tableView处于编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
//调用父类方法,实现edit和done的变换
[super setEditing:editing animated:animated];
UITableView *tableView = (UITableView *)self.view;
[tableView setEditing:editing animated:animated];
}
2.指定tableView哪些行可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
3.移动完成
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSString *str = [_mArr objectAtIndex:sourceIndexPath.row]; //引用计数加1.避免出现野指针
[str retain]; //删除元素
[_mArr removeObjectAtIndex:sourceIndexPath.row];
//插入元素
[_mArr insertObject:str atIndex:destinationIndexPath.row]; [str release];//释放之前,retain的对象
}
监测移动过程,实现限制跨区移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
NSLog(@"%d",sourceIndexPath.row);
NSLog(@"%d",proposedDestinationIndexPath.row);
if (sourceIndexPath.row == [_mArr count]-) {
return sourceIndexPath;
}else{
return proposedDestinationIndexPath;
}
}
UITableViewController表视图控制器
继承自UIViewController
自带一个tableView,根视图就是tableView
模板自带编辑移动相关的代码
UI学习笔记---第十天UITableView表视图编辑的更多相关文章
- UI学习笔记---第十一天UITableView表视图高级-自定义cell
		
自定义cell,多类型cell混合使用,cell自适应高度 自定义cell就是创建一个UITableViewCell的子类 把cell上的空间创建都封装在子类中,简化viewController中的代 ...
 - UITableView 表视图编辑
		
UITableViewController(表视图控制器)继承自UIViewController,自带一个tableView self.view不是UIView而是UITableView dataso ...
 - ui学习笔记---第十五天数据库
		
数据库的使用 常见的数据库有MySQL SQL Server SQLite Oralce等 在iOS开发中通常使用SQLite数据库,这是一个轻量级的数据库,可以在火 ...
 - JavaScript高级程序设计学习笔记第十四章--表单
		
1.在 HTML 中,表单是由<form>元素来表示的,而在 JavaScript 中,表单对应的则是 HTMLFormElement 类型. HTMLFormElement 继承了 HT ...
 - UI学习笔记---第十六天XML JSON解析
		
一.解析的基本概念 从事先规定好的格式中提取数据 解析的前提:提前约定好格式.数据提供方按照格式提供数据,数据方按照格式获取数据 常见解析方式XML解析JSON解析 二.XML:可扩展标记语言 XML ...
 - UI学习笔记---第十四天数据持久化
		
一.沙盒机制 每个应用程序位于文件系统的严格限制部分 每个应用程序只能在为该程序创建的文件系统中读取文件 每个应用程序在iOS系统内斗放在了统一的文件夹目录下 沙盘路径的位置 1. 通过Finder查 ...
 - Spring 学习笔记(十)渲染 Web 视图 (Apache Tilesa 和 Thymeleaf)
		
使用Apache Tiles视图定义布局 为了在Spring中使用Tiles,需要配置几个bean.我们需要一个TilesConfigurer bean,它会负责定位和加载Tile定义并协调生成Til ...
 - UI学习笔记---第九天UITableView表视图
		
UITableView表视图 一.表视图的使用场景 表视图UITableView是iOS中最重要的视图,随处可见,通常用来管理一组具有相同数据结构的数据 表视图继承自UIScrollView,所以可以 ...
 - VSTO学习笔记(十四)Excel数据透视表与PowerPivot
		
原文:VSTO学习笔记(十四)Excel数据透视表与PowerPivot 近期公司内部在做一种通用查询报表,方便人力资源分析.统计数据.由于之前公司系统中有一个类似的查询使用Excel数据透视表完成的 ...
 
随机推荐
- java面向对象编程--第十一章  异常处理
			
1.异常:描述出错信息的对象. 字节码校验时,如发生错误,则会抛出异常. 2.所有异常的父类是Exception,异常可以捕获,可以处理. 所有错误的父类是Error,错误可以捕获,但不能处理. Th ...
 - 一个高在线(可以超过1024)多线程的socket echo server(pthreads 和 libevent扩展)
			
研究了3周吧,本来打算用pthreads+event扩展的,结果event扩展太原始了,太多函数了,实在不知道怎么在外部随时发送数据给客户端,所以改用libevent, 改用libevent之后花了2 ...
 - POJ 3094 Quicksum 难度:0
			
http://poj.org/problem?id=3094 #include<iostream> #include <string> using namespace std; ...
 - [开发笔记]-控制Windows Service服务运行
			
用代码实现动态控制Service服务运行状态. 效果图: 代码: #region 启动服务 /// <summary> /// 启动服务 /// </summary> /// ...
 - 【C语言学习】-01 C基础
			
本文目录: 0.进制转换 1.C数据类型 2.常量变量 3.运算符 4.表达式 5.格式化输入输出 回到顶部 0.进制转换 在计算机中存储的数据,主要是以二进制形式存在,而我们生活中主要使用的有十进制 ...
 - [USACO精选] 第一章 数值计算
			
好不容易坑来了传说中的USACO精选,近100题我要是能做完就哈哈哈哈了…继今天学并查集连番受挫之后,决定写一写基础题. #0 负二进制 2014-01-10 其实是想到就会做,不想到就不会做的题,数 ...
 - DotNetBar v12.7.0.2 Fully Cracked
			
更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.7.0.2 如果遇到破解问题可以与我 ...
 - 【温故知新C/C++/opencv】取址符&||cv::groupRectangles||引用与值传递
			
cv::groupRectangles void groupRectangles(vector<Rect>& rectList, int groupThreshold, doubl ...
 - 关于wait和notify的用法
			
通常,多线程之间需要协调工作.例如,浏览器的一个显示图片的线程displayThread想要执行显示图片的任务,必须等待下载线程 downloadThread将该图片下载完毕.如果图片还没有下载完,d ...
 - Ubuntu 14.10 下安装中文输入法
			
系统默认带的是IBUS,这个不怎么好用,我们需要安装一个新的框架FCITX 1 打开软件中心,输入fcitx,安装flexible input method framework 2 下载需要的输入法, ...