**********UiTableView编辑模式, 拖动位置 ,滑动删除

#import "HMViewController.h"

@interface HMViewController () <UITableViewDataSource, UITableViewDelegate>
/** 数据列表 */
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) UITableView *tableView;
@end @implementation HMViewController - (UITableView *)tableView
{
if (_tableView == nil) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.dataSource = self;
_tableView.delegate = self; [self.view addSubview:_tableView];
}
return _tableView;
} - (NSMutableArray *)dataList
{
if (_dataList == nil) {
_dataList = [NSMutableArray arrayWithObjects:@"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwuwangwuwangwuwangwuwangwu", nil];
}
return _dataList;
} - (void)viewDidLoad
{
[super viewDidLoad]; [self tableView]; // 开始编辑,一旦editing == YES就默认开启删除模式
self.tableView.editing = YES;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataList.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
} // 设置表格
cell.textLabel.text = self.dataList[indexPath.row]; return cell;
} // 只要实现了此方法,就能够支持手势拖拽删除了,删除需要自己干!
/**
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete, 删除
UITableViewCellEditingStyleInsert 添加
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"要删除"); // MVC => 数据是保存在模型中
// 1. 删除self.dataList中indexPath对应的数据
[self.dataList removeObjectAtIndex:indexPath.row];
NSLog(@"%@", self.dataList); // 2. 刷新表格(重新加载数据)
// 重新加载所有数据
// [self.tableView reloadData];
// deleteRowsAtIndexPaths让表格控件动画删除指定的行
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
NSLog(@"要添加数据"); // 1. 向数组添加数据
[self.dataList insertObject:@"王小二" atIndex:indexPath.row + ];
// 2. 刷新表格
// [self.tableView reloadData];
// insertRowsAtIndexPaths让表格控件动画在指定indexPath添加指定行
// 新建一个indexPath
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + inSection:indexPath.section]; [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
}
} // 只要实现此方法,就可以显示拖动控件
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 界面数据UITableView已经完成了
// 调整数据即可
// [self.dataList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
// 1. 将源从数组中取出
id source = self.dataList[sourceIndexPath.row];
// 2. 将源从数组中删除
[self.dataList removeObjectAtIndex:sourceIndexPath.row];
NSLog(@"%@", self.dataList); // 3. 将源插入到数组中的目标位置
[self.dataList insertObject:source atIndex:destinationIndexPath.row]; NSLog(@"%@", self.dataList);
} #pragma mark - 代理方法
// 返回编辑样式,如果没有实现此方法,默认都是删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.row % 2) {
// return UITableViewCellEditingStyleInsert;
// } else {
// return UITableViewCellEditingStyleDelete;
// }
return UITableViewCellEditingStyleInsert;
} @end

IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)的更多相关文章

  1. ios之UITableViewController(二) tableView的编辑模式

    tableView的编辑模式 表视图可以进入编辑模式,当进入编辑模式就可以进行删除.插入.移动单元等操作 效果图: 让表视图进入编辑模式,进入编辑模式的方法有两种,一种是使用导航栏的edit 按钮,另 ...

  2. IOS开发学习笔记032-UITableView 的编辑模式

    UITableView 的三种编辑模式 1.删除 2.排序 3.添加 进入编辑模式,需要设置一个参数 - (IBAction)remove:(UIBarButtonItem *)sender { NS ...

  3. UITableView编辑模式大全解

    1.UITableView 的编辑模式 进入编辑模式 代码体现 // 设置 editing 属性 tableView?.editing = true // 这个设置的时候是有动画效果的 tableVi ...

  4. UITableView 编辑模式(增加-删除-移动---自定义左滑 title)

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  5. UITableView编辑模式

    UITableView有两种模式,普通模式和编辑模式.在编辑模式下可以对cell进行排序.删除.插入等等. 如何进入编辑模式 调用tableView的setEditing(editing: Bool, ...

  6. UITableView 编辑模式(增加-删除-移动---自定义左滑 title) xib cell

    参考:  http://www.open-open.com/lib/view/open1430008922468.html - (void)viewDidLoad { [super viewDidLo ...

  7. IOS第七天(5:UiTableView 汽车品牌,复杂模型分组展示,A-Z索要列表) (2015-08-05 14:03)

    复杂模型分组展示 #import "HMViewController.h" #import "HMCarGroup.h" #import "HMCar ...

  8. IOS第七天(3:UiTableView 模型和数据的分组的显示)

    *************UiTableView模型和数据的分组的显示 #import "HMViewController.h" #import "HMHero.h&qu ...

  9. IOS第七天(2:UiTableView 加上数据分离)

    ****加上数据分离 #import "HMViewController.h" #import "HMStudent.h" @interface HMViewC ...

随机推荐

  1. XSS 跨站脚本攻击之构造剖析(一)

    1.XSS-Filter:跨站脚本过滤器,用于分析用户提交的输入,并消除潜在的跨站脚本攻击 (1)XSS Filter实际上是一段精心编写的过滤函数作用是过滤XSS跨站脚本代码: (2)绕过XSS F ...

  2. HDU 5769 后缀数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5769 [2016多校contest-4] 题意:给定一个字符,还有一个字符串,问这个字符串存在多少个不 ...

  3. DSP using MATLAB 示例Example3.8

    代码: x = rand(1,11); n = 0:10; k = 0:500; w = (pi/500)*k; % [0,pi] axis divided into 501 points. X = ...

  4. HTTP基础07--认证

    何为认证 BASIC 认证 是从 HTTP/1.0 就定义的认证方式.即便是现在仍有一部分的网站会使用这种认证方式.是 Web 服务器与通信客户端之间进行的认证方式. 步骤 1: 当请求的资源需要 B ...

  5. css:删除:×的效果

    常常要使用的显示删除效果: DEMO

  6. JS_ECMA基本语法中的几种封装的小函数

      先来回顾一下我们的字符串: 字符串方法: str.length str.charAt(i):取字符串中的某一个; str.indexOf('e');找第一个出现的位置;找不到返回-1; str.l ...

  7. java.net.SocketException: No buffer space available

    https 访问url在调用量不大的情况下 java.net.SocketException: No buffer space available (maximum connections reach ...

  8. AC自动机(二维) UVA 11019 Matrix Matcher

    题目传送门 题意:训练指南P218 分析:一行一行的插入,一行一行的匹配,当匹配成功时将对应子矩阵的左上角位置cnt[r][c]++;然后统计 cnt[r][c] == x 的数量 #include ...

  9. ViewState与Session

    在asp时代, 大家都知道一个html控件的值,比如input 控件值,当我们把表单提交到服务器后, 页面再刷新回来的时候, input里面的数据已经被清空. 这是因为web的无状态性导致的, 服务端 ...

  10. 最长上升子序列[LIS]

    算法原理很简单,不再赘述,这里贴一个函数模板,传入的参数为序列首尾元素的指针. template<typename T> int LIS_nlogn(T * s, T * e) { ; T ...