iOS学习30之UITableView编辑
1. UITableView编辑
1> UITableView 编辑流程
2> UITableView 编辑步骤(四步)
① 第一步 : 让 TableView 处于编辑状态(在按钮点击事件方法中)
// 优化写法
// 不带动画
_rootView.tableView.editing = !_rootView.tableView.editing;
// 带动画
[_rootView.tableView setEditing:!_rootView.tableView.editing animated:YES];
② 协议设定
第二步 : 确定cell是否处于编辑状态(UITableViewDataSource协议的方法)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// 所有的cell都可以进行编辑时,整个方法可以省略
// return YES; // 只有第一个分区可以被编辑
if ( == indexPath.section) {
return YES;
}
return NO;
}
第三步 : 设定cell的编辑样式 (删除 , 添加)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyle枚举中的样式;
}
第四步 : 编辑状态进行添加
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1. 处理数据
// 2. 更新UI界面
}
3> 添加(前两步通用)
第三步:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
第四步:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1. 删除数据
[_allDataArray[indexPath.section] removeObjectAtIndex:indexPath.row]; // 2. 更新UI
// 单独更新一行(删除)
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; // 全部更新
// [tableView reloadData];
}
4> 删除(前两步通用)
第三步:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleInsert;
}
第四步:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1. 插入数据到数组中
[_allDataArray[indexPath.section] insertObject:@"你是不是傻" atIndex:indexPath.row + ]; // 2. 更新UI
// 全部更新
// [tableView reloadData]; // 单独更新一行 // 创建新一行的NSIndexPath对象
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row + inSection:indexPath.section]; [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
5>添加 和 删除 结合
第三步 : 设置一个 UITableViewCellEditingStyle 类型的 属性(style) 用于存储 添加 和 删除 的编辑的样式, 在 添加 和 删除 对应的点击事件方法中赋值, 注:按钮的功能样式需要在点击事件最上面实现, 否则会出现bug
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.style;
}
第四步 :
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 判断编辑样式
if (UITableViewCellEditingStyleDelete == editingStyle) {
删除操作,详情请见 3> 第四步
} else if (UITableViewCellEditingStyleInsert == editingStyle){
添加操作,详情请见 4> 第四步
}
}
6> 移动
① (在 TableView 处于编辑状态下)实现协议: 告诉 tableView 是否能够移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
② 移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 1. 获取需要修改的数据
NSString *sourceData = [self.allDataArray[sourceIndexPath.section] objectAtIndex:sourceIndexPath.row]; // 2. 先将数据从当前的位置移除
[self.allDataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row]; // 3. 将数据插入到对应的位置
[self.allDataArray[destinationIndexPath.section] insertObject:sourceData atIndex:destinationIndexPath.row];
}
bug修正--- 防止跨分区移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
// sourceIndexPath 为原位置
// proposedDestinationIndexPath 为将要移动到的位置
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
return proposedDestinationIndexPath;
} else {
return sourceIndexPath;
}
}
2. UITableViewController
1> 概述
UITableViewController 是继承于 UIViewController 中的一个类,只不过比UIViewController 中多了一个属性 tableView 。 即: UITableViewController 是一个自带 table 的视图控制器。
2> 注意事项
- UITableViewController 继承 UIViewController , 自带一个tableView
- self.view 不是 UIView 是 UITableView
- datasource 和 delegate 默认都是 self (UITableViewController)
- 开发中只需要建 UITableViewController 子类
iOS学习30之UITableView编辑的更多相关文章
- iOS学习——tableview中带编辑功能的cell键盘弹出遮挡和收起问题解决
最近在项目中经常用到UITableView中的cell中带有UITextField或UITextView的情况,然后在这种场景下,当我们点击屏幕较下方的cell进行编辑时,这时候键盘弹出来会出现遮挡待 ...
- iOS学习之UITableView编辑
一.UITableView编辑 UITableView编辑(删除.添加)步骤: 让TableView处于编辑状态. 协议设定:1)确定Cell是否处于编辑状态:2)设定cell的编辑样式(删除.添加) ...
- iOS学习之UITableView中Cell的操作
接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记.移动.删除.插入. 为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http ...
- iOS学习笔记之UITableViewController&UITableView
iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...
- iOS学习笔记(4) — UITableView的 重用机制
iOS学习笔记(4) — UITableView的 重用机制 UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的 ...
- iOS Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UITableView.m:7962
Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Cac ...
- IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)
**********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...
- [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading
上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...
- 2015最新iOS学习线路图
iOS是由苹果公司开发的移动操作系统,以xcode为主要开发工具,具有简单易用的界面.令人惊叹的功能,以及超强的稳定性,已经成为iPhone.iPad 和iPod touch 的强大基础:iOS 内置 ...
随机推荐
- 杨辉三角用java实现
代码如下: public class ErArray { public static void main(String[] args) { //杨辉三角 int[][] num = new int[1 ...
- XMPP框架下微信项目总结(5)花名册获取(好友列表)
---->概念 ---->添加花名册 ps:添加花名册,启动: 客户端发送请求到服务器获取好友列表信息,同时在项目中创建数据表,并保存好友列表到数据表中. ---->获取服务器保存好 ...
- Linux 查看 删除进程
这东西,时间久不用总容易忘....记下来! 1. 在 LINUX 命令平台输入 1-2 个字符后按 Tab 键会自动补全后面的部分(前提是要有这个东西,例如在装了 tomcat 的前提下, 输入 to ...
- velocity思维导图笔记
- 局域网聊天Chat(马士兵视频改进版)
Github地址: https://github.com/BenDanChen/Chat Chat 小小的聊天系统,主要是跟着网上的马士兵老师的公开视频然后再自己反思有什么地方需要改进的地方,然后大体 ...
- Linux安装xwindow图形界面
在我们安装Linux系统时,刚开始的时候可能没有安装图形界面的需要,但在使用过程中却有可能产生这种需求.那么这种情况下,我们需不需要重新安装Linux系统来安装桌面呢?答案是不需要.下面我将交大家在已 ...
- ios 拨打电话
1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString * str=[[NSMutableString alloc] initWithFo ...
- 名词解释——Ext JS4
Ext.onReady——Ext主入口,和onload事件不同,不需要页面所有东西加在出来. Ext js 的基本语法就是使用树状图来配置对象来定义界面: { config_options1:valu ...
- Delphi编译dll时出错"Cannot debug project unless a host application is defined.use the run|parameters...dialog box."
问题: 在编写DLL程序的时候,按下F9或者按下那个绿色的箭头,会报错,如下 原因: 是因为你按下的F9或者那个绿色箭头是表示“Run”这个程序,但是DLL不是可执行文件,所以当然不能够运行,所以就会 ...
- 【sicily】卡片游戏
卡片游戏 Time Limit: 1sec Memory Limit:32MB Description 桌上有一叠牌,从第一张牌(即位于顶面的牌)开始从上往下依次编号为1~n.当至少还剩两张牌 ...