IOS代码
//
// 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代码的更多相关文章
- iOS代码签名理解
前言 做了几年iOS app coder了,对于证书的生成.使用流程烂熟于心,然而对于这套机制的原理却一直不甚理解.近来由于工作需要仔细研究了一下,特将自己的学习经验记录于此,以供大家学习指正. 问题 ...
- iOS代码加密常用加密方式
iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...
- 如何把iOS代码编译为Android应用
新闻 <iPhone 6/6 Plus中国销量曝光:单月销量650万>:据iSuppli Corp.中国研究总监王阳爆料,iPhone 6和iPhone 6 Plus在国内受欢迎的情况大大 ...
- Xcode之外的文档浏览工具--Dash (在iOS代码库中浏览本帖)
链接地址:http://www.cocoachina.com/bbs/read.php?tid=273479 Xcode之外的文档浏览工具--Dash (在iOS代码库中浏览本帖) ...
- oschina iOS代码库
iOS代码库 34Activity 54下拉刷新(pull-to-refresh) 143菜单 (Menu) 20位置信息(GPS/Location) 24iOS 表单 74提醒 (Notificat ...
- 马赛克算法及iOS代码实现
之前做了一下相关涂鸦的东西,发现图片处理挺好玩的,就先讲讲马赛克的实现吧. 放大马赛克图片可以看到,可以看到一个个单色的小正方形.所以马赛克其实也就是把某一点的色值填充了它一定范围内的一个正方形,这样 ...
- 半年的iOS代码生活
半年的iOS代码生活 在高考大军中拼杀过,也在大学校园中荒芜过,曾经低迷消沉,也常满怀壮志…… 但是最多的还是被称为小伙子以及自称为iOS工程师!博主就是这种喜闻乐见的这类人,实习一年后在2015年的 ...
- 如何把设计图自动转换为iOS代码? 在线等,挺急的!
这是一篇可能略显枯燥的技术深度讨论与实践文章.如何把设计图自动转换为对应的iOS代码?作为一个 iOS开发爱好者,这是我很感兴趣的一个话题.最近也确实有了些许灵感,也确实取得了一点小成果,和大家分享一 ...
- 一篇文章看懂iOS代码块Block
block.png iOS代码块Block 概述 代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量.作为参数.作为返 ...
- (转)ios 代码规范
转自http://blog.csdn.net/pjk1129/article/details/45146955 引子 在看下面之前,大家自我检测一下自己写的代码是否规范,代码风格是否过于迥异阅读困难? ...
随机推荐
- Reactor模式
对象行为类的设计模式,对同步事件分拣和派发.别名Dispatcher(分发器) Reactor模式是处理并发I/O比较常见的一种模式,用于同步I/O,中心思想是将所有要处理的I/O事件注册到一个中心I ...
- Cocos2dx中的四种控件及主要用法
1.控件:即控制对象,控制按钮之类的精灵 2.主要介绍四大类控件: CCControlSlider:进度条 CCControlSwitch:开关 CCScale9Sprite:9妹图(用于缩放) CC ...
- 让CALayer的shadowPath跟随bounds一起做动画改变-b
在iOS开发中,我们经常需要给视图添加阴影效果,最简单的方法就是通过设置CALayer的shadowColor.shadowOpacity.shadowOffset和shadowRadius这几个属性 ...
- C++拷贝构造函数详解(转载)
一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: int a = 100; int b = a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员 ...
- 【贪心】Bzoj 2457:[BeiJing2011]双端队列
2457: [BeiJing2011]双端队列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 209 Solved: 95[Submit][Stat ...
- nenu contest2
http://vjudge.net/vjudge/contest/view.action?cid=54562#overview H B. Polygons http://codeforces.com ...
- Taxi Trip Time Winners' Interview: 3rd place, BlueTaxi
Taxi Trip Time Winners' Interview: 3rd place, BlueTaxi This spring, Kaggle hosted two competitions w ...
- hdu 1166 树状数组 线段树入门
点修改 区间求和 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...
- 为什么乱码:<meta http-equiv="content-type">前的非ANSI字符
为什么乱码:<meta http-equiv="content-type">前的非ANSI字符 浏览器检测网页字符集的默认顺序 浏览器的网页字符集检测顺序通常是: ch ...
- 6 个基于 jQuery 的表单向导插件推荐
表单向导可以很好地引导用户进行一步一步的操作,从而降低用户错误输入的几率.尽管互联网中有大量的类似插件,但真正好用的不多. 本文整理了6个比较优秀的表单向导插件,希望能够为你带来帮助. 1. Smar ...