猫猫学IOS(十四)UI之UITableView扩充_表格的修改_(增删移动)
来源:互联网 发布:mac推出磁盘快捷键 编辑:程序博客网 时间:2024/06/08 14:09
猫猫分享,必须精品
素材代码地址:http://blog.csdn.net/u013357243/article/details/44727823
原文地址:http://blog.csdn.net/u013357243?viewmode=contents
先看效果图
代码
//ps:新建iOS交流学习群:304570962 可以加猫猫QQ:1764541256 或则微信znycat 让我们一起努力学习吧。 原文:http://blog.csdn.net/u013357243?viewmode=contents// HMViewController.m// 06-表格的修改//// 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);}
ps:新建iOS交流学习群:304570962 可以加猫猫QQ:1764541256 或则微信znycat 让我们一起努力学习吧。 原文:http://blog.csdn.net/u013357243?viewmode=contents
11 0
- 猫猫学IOS(十四)UI之UITableView扩充_表格的修改_(增删移动)
- (素材源码)猫猫学IOS(十四)UI之UITableView扩充_表格的修改_(增删移动)
- AJ学IOS(14)UI之UITableView扩充_表格的修改_(增删移动)
- 猫猫学IOS(八)UI之热门_喜马拉雅UI实现-UIScrollView的使用
- 猫猫学IOS(三十六)UI之手势事件旋转_缩放_拖拽
- (素材源码)猫猫学IOS(三十六)UI之手势事件旋转_缩放_拖拽
- (素材源码)猫猫学IOS(八)UI之热门_喜马拉雅UI实现-UIScrollView的使用
- AJ学IOS(08)UI之热门_喜马拉雅UI实现-UIScrollView的使用
- (素材源码)猫猫学IOS(六)UI之iOS热门游戏_超级猜图
- 猫猫学IOS(六)UI之iOS热门游戏_超级猜图
- 猫猫学IOS(十八)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复
- UitableView的增删和移动
- (素材源码)猫猫学IOS(七)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
- 猫猫学IOS(七)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
- AJ学IOS(36)UI之手势事件旋转_缩放_拖拽
- UITableView 编辑(增删移动)
- (素材源码)猫猫学IOS(九)UI之UIScrollView代理触摸实现_图片缩放
- 猫猫学IOS(九)UI之UIScrollView代理触摸实现_图片缩放
- 第二章作业
- IMS SIP相关的Android终端的想法----来自实验室好友王阳
- IO概述
- NSDictionary、NSMutableDictionary的基本用法
- 项目经理人际关系技能
- 猫猫学IOS(十四)UI之UITableView扩充_表格的修改_(增删移动)
- C# 值类型和引用类型
- Python2.x和3.x主要差异总结
- 用struct模块实现python socket收发自定义TCP包
- java延时设置
- struts1.3中<struts-config>报错
- linux下mysql的卸载过程
- 【Python笔记】剖析Python的切片(slicing)语法
- 白盒测试的工具CPPtest使用