@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. RabbitMQ-死信(Dead Letter)

    对于有异常的消息我们可以有如下做法: 记录下来再ack. nack或者reject,同时将requeue设为false. 在第2条的基础上增加死信(Dead Letter).   上边的第3个做法可以 ...

  2. continuous integration and continuous deployment in DW/BI

    deployment methodIn Redshift1, Deploy process: Drop and Refresh the view, Drop table, Create an empt ...

  3. Effect of Switchovers, Failovers, and Control File Creation on Backups

    对dataguard 官方文档里面的这句话不理解,是否能给出一个样例说明: 10.2.0.5的版本号 Effect of Switchovers, Failovers, and Control Fil ...

  4. 关于Yaffs2在u-boot中的支持

    开发板是一块2G的MLC的NandFlash,页大小8k+512,为其移植u-boot到yaffs2这了.以前在Mini2440上移植过2k+64的slc的NandFlash的Yaffs2支持,当然也 ...

  5. poj 3735 大数量反复操作问题(矩阵高速幂)

    题意:一个一维数组,3种操作: a:  第i个数+1,b: 第i个数=0 ,c::交换某俩处的数.  由三种基本操作构成一组序列,反复该序列m次(m<10^9),问结果 属于一种综合操作反复型: ...

  6. Javascript 控制style 小结

    style.top 如: c.style.top=scrollTop; 在IE各版本中可以,Safari, chrome, Firefox都不work, 需要在后面 + "px";

  7. MVC模式下的数据展示:EasyUI的datagrid

    我的数据库设计是一张老师表teacher,一张学生表student,一个教师对应多个学生,在学生一方建立外键; 还有一点想清楚,需要展示的数据是根据什么来的,是成功登陆的用户的id?还是直接展示所有的 ...

  8. 初学coreData数据库读取不成功的问题

    写了一个从数据库读取数据显示列表的代码,结果却无法运行,提示找不到对应的entity,也就是数据库中的某一个表 我查遍了代码也没有发现什么逻辑错误,在appDelegate也初始化了相关数据库,在界面 ...

  9. Android(java)学习笔记244:多媒体之SurfaceView

    1. SurfaceView:     完成单位时间内界面的快速切换(游戏界面流畅感). 我们之前知道一般的View,只能在主线程里面显示,主线程中更新UI.但是SurfaceView可以在子线程中里 ...

  10. 登录网站爬虫(保持Cookie不变)

    平时经常需要到学校的信息门户去查看课表及其他信息,于是想做一个爬虫 ,可以自动替我登录并且得到这些信息,于是今天动手写了一个爬虫: 首先登录学校的信息门户:http://cas.whu.edu.cn/ ...