@interface MJViewController () <UITableViewDataSource, UITableViewDelegate>

{

NSMutableArray *_persons;

}

@end

@implementation MJViewController

- (void)viewDidLoad

{

[super viewDidLoad];

_persons = [NSMutableArray array];

for (int i = 0; i<30; i++) {

Person *p = [[Person alloc] init];

p.name = [NSString stringWithFormat:@"Person---%d", i];

p.phone = [NSString stringWithFormat:@"%d", 10000 + arc4random_uniform(10000000)];\

[_persons addObject:p];

}

}

#pragma mark - 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return _persons.count;

}

#pragma mark 每一行显示怎样的cell(内容)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

// 1.定义一个标识

static NSString *ID = @"cell";

// 2.去缓存池中取出可循环利用的cell

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

// 3.如果缓存中没有可循环利用的cell

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];

}

// 4.设置数据

// 4.1.取出模型

Person *p = _persons[indexPath.row];

// 4.2.姓名

cell.textLabel.text = p.name;

// 4.3.手机

cell.detailTextLabel.text = p.phone;

return cell;

}

#pragma mark - 代理方法

#pragma mark 当用户提交了一个编辑操作就会调用(比如点击了“删除”按钮)

// 只要实现了这个方法,就会默认添加滑动删除功能

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

// 如果不是删除操作,直接返回

if (editingStyle != UITableViewCellEditingStyleDelete) return;

// 1.删除模型数据

[_persons removeObjectAtIndex:indexPath.row];

// 2.刷新表格

//    [tableView reloadData];

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

}

#pragma mark 当移动了某一行cell就会调用

// 只要实现了这个方法,就会默认添加排序功能

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

//    NSLog(@"%d --- %d", sourceIndexPath.row, destinationIndexPath.row);

//    [_persons exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

// 1.取出要拖动的模型数据

Person *p = _persons[sourceIndexPath.row];

// 2.删除之前行的数据

[_persons removeObject:p];

// 3.插入数据到新的位置

[_persons insertObject:p atIndex:destinationIndexPath.row];

}

#pragma mark 删除

- (IBAction)remove:(id)sender {

// 1.进入编辑模式

//    self.tableView.editing = YES;

BOOL result = !self.tableView.isEditing;

[self.tableView setEditing:result animated:YES];

}

@end

TableView_编辑 实例代码的更多相关文章

  1. python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息,抓取政府网新闻内容

    python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息PySpider:一个国人编写的强大的网络爬虫系统并带有强大的WebUI,采用Python语言编写 ...

  2. jquery-easyui combobox combogrid 级联不可编辑实例

    jquery-easyui combobox combogrid 级联不可编辑实例 如何让jquery-easyui的combobox像select那样不可编辑?为combobox添加editable ...

  3. 功能强大的图片截取修剪神器:Android SimpleCropView及其实例代码重用简析(转)

    功能强大的图片截取修剪神器:Android SimpleCropView及其实例代码重用简析 SimpleCropView是github上第一个第三方开源的图片修剪截取利器,功能强大,设计良好.我个人 ...

  4. C++11 变长模版和完美转发实例代码

    C++11 变长模版和完美转发实例代码 #include <memory>#include <iostream>#include <vector>#include ...

  5. PHP读取超大文件的实例代码

    数据量大带来的问题就是单个文件很大,能够打开这个文件相当不容易,记事本就不要指望了,果断死机   去年年底的各种网站帐号信息的数据库泄漏,很是给力啊,趁机也下载了几个数据库,准备学学数据分析家来分析一 ...

  6. python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例

    python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...

  7. junit基础篇、中级篇-实例代码

    学习文章: http://blog.csdn.net/andycpp/article/details/1327147 http://wenku.baidu.com/link?url=C27gDEj0l ...

  8. C#开发中使用Npoi操作excel实例代码

    C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...

  9. seo之google rich-snippets丰富网页摘要结构化数据(微数据)实例代码

    seo之google rich-snippets丰富网页摘要结构化数据(微数据)实例代码 网页摘要是搜索引擎搜索结果下的几行字,用户能通过网页摘要迅速了解到网页的大概内容,传统的摘要是纯文字摘要,而结 ...

随机推荐

  1. Http报文 状态码

    一.HTTP报文 1.HTTP报文介绍 HTTP报文:用于HTTP协议交互的信息. HTTP报文由报文头部和报文主体(非必须)构成,中间由空行来分隔. 1.1 请求报文:客户端发起的报文. 1).报文 ...

  2. html中em和px

    px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的. em是相对长度单位.相对于当前对象内文本的字体尺寸.如当前对行内文本的字体尺寸未被人为设置,则相对于浏览器的默认字体尺寸 ...

  3. java hashCode()与equals()的作用

    1.hashcode是用来查找的,如果你学过数据结构就应该知道,在查找和排序这一章有 例如内存中有这样的位置 0  1  2  3  4  5  6  7 而我有个类,这个类有个字段叫ID,我要把这个 ...

  4. TextView的ems和MaxLength

    <EditText android:id="@+id/et_code" android:layout_width="wrap_content" andro ...

  5. Flashback Query、Flashback Table(快速闪回查询、快速闪回表)

    Flashback Query闪回查询 flashback query是基于undo表空间的闪回,与之相关的参数如下: SQL> show parameter undo NAME         ...

  6. [WebStrom] Change default cmd to Cygwin

    GO to setting, search Terminal: Change shell path : C:\cygwin\bin\bash.exe --login -i    (to the loc ...

  7. android strings.xml转义字符, 注意细节解决(转)

    XML转义字符 以下为XML标志符的数字和字符串转义符 "     (" 或 ") '     (' 或 &apos;) &     (& 或 & ...

  8. JFreeChart当鼠标停留在热点提示自定义信息功能

    当鼠标停留在数据点时,希望有提示信息显示,但是根据业务需要,我们需要自定义显示一下信息!具体功能实现如下: 首先我们要弄明白,Jfreechart鼠标提示信息显示主要是得到一个数据的map对象,该ma ...

  9. ListView滑动删除

    本来准备在ListView的每个Item的布局上设置一个隐藏的Button,当滑动的时候显示.但是因为每次只要存在一个Button,发现每个Item上的Button相互间不好控制.所以决定继承List ...

  10. input输入过滤js

    html部分使用方式 <input  onkeyup="usrNameSet(this)" /> 其它的自己可以随便调用 Js部分 //只能输入数字.字母.小数点.汉字 ...