@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. 输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为O(n)

    群里看到这道题,用python做了做, def find(array): v_sum = greatest = 0 for a in array: v_sum += a v_sum = 0 if v_ ...

  2. 3D游戏引擎一 win32编程

    Windows程序一般都等待用户进行一些操作,然后响应并採取行动. 一般来说.对win32的程序的操作都会转换为系统事件队列中的消息,如按键消息WM_KEYDOWN,WM_MOUSECLICK等传递键 ...

  3. 边走边学Nodejs (基础入门篇)

    1.什么是Node.js Nodejs ,或者node, 是一个基于ChromeJavaScript执行时建立的平台.用于方便地搭建响应速度快.易于扩展的网络应用.Node.js 使用事件驱动, 非堵 ...

  4. 没有懂的leetcode

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. IP 转地址

    1.需要  QQWry.Dat IP 地址数据库 2辅助类库 using System; using System.Collections.Generic; using System.IO; usin ...

  6. Android界面刷新方法

    Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中 ...

  7. Wpf Binding.Path设置

    Binding.Path 获取或设置绑定源属性的路径. 每个绑定通常都具有四个组件:绑定目标对象.目标属性.绑定源,以及要使用的绑定源值的路径.有关这些数据绑定概念的更多信息,请参见数据绑定概述. 使 ...

  8. 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序(Oledb)

    转载:http://blog.csdn.net/lemontec/article/details/1754413 前几天用c#读 Excel 出现了如下问题未在本地计算机上注册“Microsoft.J ...

  9. JqueryAjax

    1. 2. 3. 4. 5. 6. 7.

  10. iOS子线程操作UI问题检查

    iOS开发中,因为大部分函数都不是线程安全的,所以UI子线程中操作UI是非常危险的事,但是有时候因为开发者经验不足,不知道子线程中不能UI,或者知道但是写代码的时候没注意,或者不知道那些函数操作UI了 ...