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 内置 ...
随机推荐
- Cocoapods的使用教程
前言 对于iOS App的开发,几乎都采用了Cocoapods来管理第三方库,那么对于我们开发人员来说,这是必备技能,必须要掌握如何使用.这篇文章就是介绍如何安装和使用CocoaPods的. 这篇文章 ...
- NYOJ题目1162数字
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr8AAAJ/CAIAAAD+kywNAAAgAElEQVR4nO3dO1LjzN4H4G8T5CyE2A ...
- jsp 过滤器 Filter 配置
.如果要映射过滤应用程序中所有资源: <filter> <filter-name>loggerfilter</filter-name> <filt ...
- hadoop2.x NameNode 的共享存储实现
过去几年中 Hadoop 社区涌现过很多的 NameNode 共享存储方案, 比如 shared NAS+NFS.BookKeeper.BackupNode 和 QJM(Quorum Journal ...
- 无废话Android之常见adb指令、电话拨号器、点击事件的4种写法、短信发送器、Android 中各种布局(1)
1.Android是什么 手机设备的软件栈,包括一个完整的操作系统.中间件.关键的应用程序,底层是linux内核,安全管理.内存管理.进程管理.电源管理.硬件驱动 2.Dalvik VM 和 JVM ...
- JavaWeb学习之JSP常用标签、EL表达式的运算符、JSTL标签库(6)
1.JSP常用标签 * 只要支持JSP文件,常用标签有可以直接使用 * 格式: jsp:xxxx * jsp:forward ,完成jsp页面的转发 * page属性:转发的地址 <% requ ...
- 排队打饭 sdut 2443【最简单的贪心法应用举例】
排队打饭 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/p ...
- Sublime Text3 配置Node.js运行命令
在Sublime Text中可以很容易配置新的编译运行命令,下面的截图是汉化版的中文菜单,英文菜单请直接对照. 首先需要在本地安装Node,默认的Node会加入到系统的环境变量,这样执行Node命 ...
- Lucene.net 全文检索 盘古分词
lucene.net + 盘古分词 引用: 1.Lucene.Net.dll 2.PanGu.Lucene.Analyzer.dll 3.PanGu.HighLight.dll 4.PanGu.dll ...
- VIM学习笔记
参考: http://linux.chinaunix.net/techdoc/beginner/2009/12/20/1150108.shtml VIM命令大全 光标控制命令 命令 ...