AJ分享,必须精品

先看效果图

代码


//
// Created by apple on 14-8-19.
// Copyright (c) 2014年 itcast. All rights reserved.
// #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:@"猫猫1号", @"猫猫1号", @"猫猫2号", @"猫猫3号", @"猫猫4号", @"猫猫5号",@"猫猫6号", @"猫猫7号", @"猫猫8号",@"猫猫9号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",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 + 1];
// 2. 刷新表格
// [self.tableView reloadData];
// insertRowsAtIndexPaths让表格控件动画在指定indexPath添加指定行
// 新建一个indexPath
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 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

UITableView支持删除手势

只要实现了此方法,就能够支持手势拖拽删除了,删除需要自己干!

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

UITableViewCellEditingStyleNone, 没效果
UITableViewCellEditingStyleDelete, 删除
UITableViewCellEditingStyleInsert 添加

删除中要做的:
重新加载数据时候用[self.tableView reloadData];会效率低下

// MVC => 数据是保存在模型中
// 1. 删除self.dataList中indexPath对应的数据
[self.dataList removeObjectAtIndex:indexPath.row];
NSLog(@"%@", self.dataList); // 2. 刷新表格(重新加载数据)
// 重新加载所有数据
// [self.tableView reloadData];
// deleteRowsAtIndexPaths让表格控件动画删除指定的行
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];

UITableView 增加

要实现守代理方法 tableView.delegate = self;

// 返回编辑样式,如果没有实现此方法,默认都是删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.row % 2) {
// return UITableViewCellEditingStyleInsert;
// } else {
// return UITableViewCellEditingStyleDelete;
// }
return UITableViewCellEditingStyleInsert;
}

下面是在- (void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath 方法中设置的
判断编辑样式,是删除还是增添,然后做相应操作

 if (editingStyle == UITableViewCellEditingStyleInsert) {
NSLog(@"要添加数据"); // 1. 向数组添加数据
[self.dataList insertObject:@"旺旺旺旺狗狗" atIndex:indexPath.row + 1];
// 2. 刷新表格
// [self.tableView reloadData];
// insertRowsAtIndexPaths让表格控件动画在指定indexPath添加指定行
// 新建一个indexPath
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section]; [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];

UITableView 移动

// 只要实现此方法,就可以显示拖动控件
- (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);
}

最新文章

  1. forEach遍历数组实例
  2. 警惕多iframe下的同名id引起的诡异问题
  3. Kafka+Storm+HDFS整合实践
  4. BZOJ 1146: [CTSC2008]网络管理Network 树链剖分+线段树+平衡树
  5. poj3275
  6. HNU OJ10320 穿越火线 简单模拟
  7. 暑假集训(1)第六弹 -----简单计算器(Hdoj1237)
  8. Java NIO——Selector机制源码分析---转
  9. Facebook 调试工具Stetho配置入门
  10. 同花顺核新下单程序的&quot;界面不操作超时时间&quot;的设定
  11. Hive基础(5)---内部表 外部表 临时表
  12. 《JAVA程序设计》_第五周学习总结
  13. js获取世界不同时区的当前时间
  14. centos7下安装docker(21docker swarm集群创建)
  15. BIM开发引挈
  16. Touch事件详解及区别,触屏滑动距离计算
  17. 【java】:Junit
  18. vue 组件发布记录
  19. camunda 开源的bpm系统
  20. JS学习 - offset家族(一)

热门文章

  1. Linux下反弹shell笔记
  2. oracle数据库表用序列实现主键自增长
  3. Java进阶之心态
  4. Django-rest-framework源码分析(三)
  5. Spring Boot 邮件发送
  6. iOS 预渲染加速图像显示
  7. TensorFlow报错module &#39;tensorflow&#39; has no attribute &#39;xxx&#39;解决办法
  8. CtenOS开放3306端口
  9. MATLAB—地图
  10. Android的安装