//
// MJViewController.m
// UITableView-编辑模式
//
// Created by mj on 13-4-11.
// Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJViewController.h"

@interface MJViewController () {
// 当前的编辑模式
UITableViewCellEditingStyle _editingStyle;
}
@property (nonatomic, retain) NSMutableArray *data;
@end

@implementation MJViewController
#pragma mark - 生命周期方法
- (void)viewDidLoad
{
[super viewDidLoad];
self.data = [NSMutableArray array];

for (int i = 0; i<20; i++) {
NSString *text = [NSString stringWithFormat:@"mj-%i", i];
[self.data addObject:text];
}

// 设置tableView可不可以选中
//self.tableView.allowsSelection = NO;

// 允许tableview多选
//self.tableView.allowsMultipleSelection = YES;

// 编辑模式下是否可以选中
//self.tableView.allowsSelectionDuringEditing = NO;

// 编辑模式下是否可以多选
//self.tableView.allowsMultipleSelectionDuringEditing = YES;

// 获取被选中的所有行
// [self.tableView indexPathsForSelectedRows]

// 获取当前可见的行
// [self.tableView indexPathsForVisibleRows];
}

- (void)viewDidUnload {
[super viewDidUnload];
self.data = nil;
}

- (void)dealloc {
[_data release];
[super dealloc];
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"UITableViewCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
cell.detailTextLabel.text = @"详细描述";
}

cell.textLabel.text = [self.data objectAtIndex:indexPath.row];

return cell;
}

#pragma mark - 代理方法
#pragma mark 设置Cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// NSLog(@"didSelectRowAtIndexPath");
//}

#pragma mark 提交编辑操作时会调用这个方法(删除,添加)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// 删除操作
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 1.删除数据
[self.data removeObjectAtIndex:indexPath.row];

// 2.更新UITableView UI界面
// [tableView reloadData];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
} else {
// 添加操作

// 1.添加数据
int row = indexPath.row + 1;
[self.data insertObject:@"新添加的数据" atIndex:row];

// 2.更新UI界面
//[tableView reloadData];
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}

#pragma mark 决定tableview的编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return _editingStyle;
}
#pragma mark 只有实现这个方法,编辑模式中才允许移动Cell
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
// NSLog(@"from(%i)-to(%i)", sourceIndexPath.row, destinationIndexPath.row);
// 更换数据的顺序
[self.data exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}

#pragma mark - 公共方法
#pragma mark 删除数据
- (void)deleteData {
_editingStyle = UITableViewCellEditingStyleDelete;

// 开始编辑模式
// self.tableView.editing = YES;
// [self.tableView setEditing:YES];

BOOL isEditing = self.tableView.isEditing;
// 开启\关闭编辑模式
[self.tableView setEditing:!isEditing animated:YES];
}

#pragma mark 添加数据
- (void)addData {
_editingStyle = UITableViewCellEditingStyleInsert;

BOOL isEditing = self.tableView.isEditing;
// 开启\关闭编辑模式
[self.tableView setEditing:!isEditing animated:YES];
}
@end

IOS代码的更多相关文章

  1. iOS代码签名理解

    前言 做了几年iOS app coder了,对于证书的生成.使用流程烂熟于心,然而对于这套机制的原理却一直不甚理解.近来由于工作需要仔细研究了一下,特将自己的学习经验记录于此,以供大家学习指正. 问题 ...

  2. iOS代码加密常用加密方式

    iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...

  3. 如何把iOS代码编译为Android应用

    新闻 <iPhone 6/6 Plus中国销量曝光:单月销量650万>:据iSuppli Corp.中国研究总监王阳爆料,iPhone 6和iPhone 6 Plus在国内受欢迎的情况大大 ...

  4. Xcode之外的文档浏览工具--Dash (在iOS代码库中浏览本帖)

    链接地址:http://www.cocoachina.com/bbs/read.php?tid=273479 Xcode之外的文档浏览工具--Dash    (在iOS代码库中浏览本帖)       ...

  5. oschina iOS代码库

    iOS代码库 34Activity 54下拉刷新(pull-to-refresh) 143菜单 (Menu) 20位置信息(GPS/Location) 24iOS 表单 74提醒 (Notificat ...

  6. 马赛克算法及iOS代码实现

    之前做了一下相关涂鸦的东西,发现图片处理挺好玩的,就先讲讲马赛克的实现吧. 放大马赛克图片可以看到,可以看到一个个单色的小正方形.所以马赛克其实也就是把某一点的色值填充了它一定范围内的一个正方形,这样 ...

  7. 半年的iOS代码生活

    半年的iOS代码生活 在高考大军中拼杀过,也在大学校园中荒芜过,曾经低迷消沉,也常满怀壮志…… 但是最多的还是被称为小伙子以及自称为iOS工程师!博主就是这种喜闻乐见的这类人,实习一年后在2015年的 ...

  8. 如何把设计图自动转换为iOS代码? 在线等,挺急的!

    这是一篇可能略显枯燥的技术深度讨论与实践文章.如何把设计图自动转换为对应的iOS代码?作为一个 iOS开发爱好者,这是我很感兴趣的一个话题.最近也确实有了些许灵感,也确实取得了一点小成果,和大家分享一 ...

  9. 一篇文章看懂iOS代码块Block

    block.png iOS代码块Block 概述 代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量.作为参数.作为返 ...

  10. (转)ios 代码规范

    转自http://blog.csdn.net/pjk1129/article/details/45146955 引子 在看下面之前,大家自我检测一下自己写的代码是否规范,代码风格是否过于迥异阅读困难? ...

随机推荐

  1. STL算法

    STL算法部分主要由头文 件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<algorit ...

  2. iOS7之定制View Controller切换效果

    在iOS5和iOS6前,View Controller的切换主要有4种: 1. Push/Pop,NavigationViewController常干的事儿 2. Tab,TabViewControl ...

  3. Java反射在JVM的实现

    1. 什么是Java反射,有什么用?反射使程序代码能够接入装载到JVM中的类的内部信息,允许在编写与执行时,而不是源代码中选定的类协作的代码,是以开发效率换运行效率的一种手段.这使反射成为构建灵活应用 ...

  4. UIViewController没有随着设备一起旋转的原因

    对于iPhone app,UIViewController类提供了基本的视图管理模式.当设备改变方向的时候view controller的视图会自动随之旋转的.如果视图和子视图的autoresizin ...

  5. DVDRW光驱无法读DVD刻录盘

    原文地址:http://www.douban.com/note/206741292/ 所有的DVD RW驱动器,不能识别DVD ROM光盘问题,都是因为DVD区域没有给定造成的,除非设备损坏!在计算机 ...

  6. Careercup - Facebook面试题 - 4909367207919616

    2014-05-01 01:23 题目链接 原题: WAP to modify the array such that arr[I] = arr[arr[I]]. Do this in place i ...

  7. C#在Winform中改变Textbox高度三种方法

    最近在做C# Winform项目,需要有一个能动态调整大小的Textbox,并且要是单行的.试了几次,单行模式的Textbox不能直接改高度.于是搜索了一下,整理出几个改变高度的方法. 1.将Text ...

  8. P1676陶陶吃苹果 - vijos

    描述 curimit知道陶陶很喜欢吃苹果.于是curimit准备在陶陶生日的时候送给他一棵苹果树. curimit准备了一棵这样的苹果树作为生日礼物:这棵苹果树有n个节点,每个节点上有c[i]个苹果, ...

  9. python爬取某些网站出错的解决办法

    用urllib2.urlopen方法打开糗事百科的网站,http://www.qiushibaike.com/,发现会失败,网上百度,说可能是服务器端对爬虫做了屏蔽,需要伪装header头部信息,下面 ...

  10. [转载]eclipse中设置文件的编码格式为utf-8

    免责声明:     本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除.     原文作者:ryxxlong     原文地址:http://ryxxlong.iteye ...