iOS - UITableView 编辑(cell的插入, 删除, 移动)
UITableView Cell的插入/删除
核心API
Class : UITableView
Delegate : UITableViewDataSource, UITableViewDelegate
涉及的API:(API的官方详细注释详见本章结尾)
/** TableView 进入或退出编辑状态(TableView 方法). */
- (void)setEditing:(BOOL)editing animated:(BOOL)animate /** 确定哪些行的cell可以编辑 (UITableViewDataSource协议中方法). */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath /** 设置某一行cell的编辑模式 (UITableViewDelegate协议中方法). */
TableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath /** 提交编辑状态 (UITableViewDataSource协议中方法). */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath /** 插入 cell (UITableView 方法). */
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation /** 删除 cell (UITableView 方法). */
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
功能实现
思路:
1.让TableView 进入编辑状态
2.指定哪些 cell 可以进行编辑
3.指定cell的编辑状态(删除还是插入)
4.选中删除(或插入)状态之后的操作(数据源进行更新, cell删除或插入)
Code:
1 . 让TableView 进入编辑状态 (UIViewControll.m)
/** 当点击UINavigationBar 上面系统提供的编辑按钮的时候, 系统会调用这个方法. */
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
/** 首先调用父类的方法. */
[super setEditing:editing animated:animated]; /** 使tableView处于编辑状态. */
[self.tableView setEditing:editing animated:animated]; }
2. 指定哪些行的 cell 可以进行编辑 (UITableViewDataSource 协议方法)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if (0 == indexPath.row) {
return NO; /**< 第一行不能进行编辑. */
} else {
return YES;
}
}
3.指定cell的编辑状态(删除还是插入) (UITableViewDelegate 协议方法)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
/** 不同的行, 可以设置不同的编辑样式, 编辑样式是一个枚举类型 */
if (indexPath.row == 0) {
return UITableViewCellEditingStyleInsert;
} else {
return UITableViewCellEditingStyleDelete;
}
}
4.选中删除(或插入)状态之后的操作(数据源进行更新, cell删除或插入) (UITableViewDataSource 协议方法)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
/** 点击 删除 按钮的操作 */
if (editingStyle == UITableViewCellEditingStyleDelete) { /**< 判断编辑状态是删除时. */ /** 1. 更新数据源(数组): 根据indexPaht.row作为数组下标, 从数组中删除数据. */
[self.arr removeObjectAtIndex:indexPath.row]; /** 2. TableView中 删除一个cell. */
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
} /** 点击 +号 图标的操作. */
if (editingStyle == UITableViewCellEditingStyleInsert) { /**< 判断编辑状态是插入时. */
/** 1. 更新数据源:向数组中添加数据. */
[self.arr insertObject:@"abcd" atIndex:indexPath.row]; /** 2. TableView中插入一个cell. */
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }
}
UITableView cell 的移动
核心API
Class: UITableView
Deletage: UITableViewDataSource, UITableViewDelegate
涉及的API:(API的官方详细注释详见本章结尾)
/** TableView 进入或退出编辑状态(TableView 方法). */
- (void)setEditing:(BOOL)editing animated:(BOOL)animate /** 指定 tableView 哪些行(cell) 可以移动. */
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath /** 移动 cell. */
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
功能实现
思路:
1.让 TableView 进入或退出 编辑状态
2.指定 tableView 哪些行(cell) 可以移动
3.移动 cell 后的操作: 数据源进行更新
Code
1 . 让 TableView 进入或退出 编辑状态
/** 当点击UINavigationBar 上面系统提供的编辑按钮的时候, 系统会调用这个方法. */
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
/** 首先调用父类的方法. */
[super setEditing:editing animated:animated]; /** 使tableView处于编辑状态. */
[self.tableView setEditing:editing animated:animated];
}
2.指定 tableView 哪些行(cell) 可以移动 (UITableViewDataSource协议方法)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
/** 指定哪些行(cell)可以移动 */
if (0 == indexPath.row) {
return NO; /**< NO cell不能移动 */
} else {
return YES; /**< YES cell可以移动 */
}
}
3.移动 cell 后的操作: 数据源进行更新
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
/** 1. 从原位置移除,在从原位置移除之前, 需要保存一下原位置的数据, 同时持有一次. */
NSString *str = [[self.arr objectAtIndex:sourceIndexPath.row] retain]; [self.arr removeObjectAtIndex:sourceIndexPath.row]; /** 2. 添加到目的位置, 同时释放一次 */
[self.arr insertObject:str atIndex:destinationIndexPath.row];
[str release];
}
API 官方注释
/**
* @brief Asks the data source to commit the insertion or deletion of a specified row in the receiver.
*
* @param <tableView> The table-view object requesting the insertion or deletion.
* @param <editingStyle> The cell editing style corresponding to a insertion or deletion requested for the row specified by indexPath. Possible editing styles are UITableViewCellEditingStyleInsert or UITableViewCellEditingStyleDelete.
* @param <indexPath> An index path locating the row in tableView.
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
/**
* @brief Asks the data source to verify that the given row is editable.
*
* @param <tableView> The table-view object requesting this information.
* @param <indexPath> An index path locating a row in tableView.
*
* @return YES if the row indicated by indexPath is editable; otherwise, NO.
*/
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
/**
* @brief Inserts rows in the table view at the locations identified by an array of index paths, with an option to animate the insertion.
*
* @param <indexPaths> An array of NSIndexPath objects, each representing a row index and section index that together identify a row in the table view.
* @param <animation> A constant that either specifies the kind of animation to perform when inserting the cell or requests no animation. See Table Cell Insertion and Deletion Animation for descriptions of the constants.
*
**/ - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
/**
* @brief Deletes the rows specified by an array of index paths, with an option to animate the deletion.
*
* @param <indexPaths> An array of NSIndexPath objects identifying the rows to delete.
* @param <animation> A constant that indicates how the deletion is to be animated, for example, fade out or slide out from the bottom. See Table Cell Insertion and Deletion Animation for descriptions of these constants.
*
*/
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
iOS - UITableView 编辑(cell的插入, 删除, 移动)的更多相关文章
- iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法
"UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...
- ios UITableView中Cell重用机制导致内容重复解决方法
UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...
- ios cell左滑删除
iOS项目开发小技能 (三) -UITableView实现Cell左划删除等自定义功能 www.MyException.Cn 网友分享于:2015-06-05 浏览:0次 iOS项目开发小技巧 ...
- IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)
**********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...
- iOS学习30之UITableView编辑
1. UITableView编辑 1> UITableView 编辑流程 2> UITableView 编辑步骤(四步) ① 第一步 : 让 TableView 处于编辑状态(在按钮点击事 ...
- iOS学习之UITableView编辑
一.UITableView编辑 UITableView编辑(删除.添加)步骤: 让TableView处于编辑状态. 协议设定:1)确定Cell是否处于编辑状态:2)设定cell的编辑样式(删除.添加) ...
- UI:UITableView 编辑、cell重用机制
tableView编辑.tableView移动.UITableViewController tableView的编辑:cell的添加.删除. 使⽤场景: 删除⼀个下载好的视频,删除联系⼈: 插⼊⼀条新 ...
- iOS UITableView划动删除的实现
标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...
- 设置UITableView背景透明/监听cell左边的删除按钮的点击事件
_tableView = [[UITableView alloc] init]; _tableView.delegate = self; _tableView.dataSource = self; _ ...
随机推荐
- composer 更新指定包
1)网上搜了大半天都不知道怎么更新 componser 包,update upgrade 命令根本不知道怎么用!! 2)其实用 require 命令就可以更新包(它会判断包存不存在,不存在就安装,存在 ...
- Explaining Delegates in C# - Part 2 (Events 1)
In my previous post, I spoke about a few very basic and simple reasons of using delegates - primaril ...
- Unity绘制Png图片
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; publ ...
- 利用Squid反向代理搭建CDN缓存服务器加快Web访问速度
2011年11月26日 ? Web服务器架构 ? 评论数 2 案例:Web服务器:域名www.abc.com IP:192.168.21.129 电信单线路接入访问用户:电信宽带用户.移动宽带用户出现 ...
- Android中实现定时器的3中方法
在Android开发中,定时器一般有以下3种实现方法: 一.采用Handler与线程的sleep(long)方法: 二.采用Handler的postDelayed(Runnable, long)方法: ...
- C语言EOF是什么?
C语言 EOF是什么? Linux中,在新的一行的开头,按下Ctrl-D,就代表EOF(如果在一行的中间按下Ctrl-D,则表示输出"标准输入"的缓存区,所以这时必须按两次Ctrl ...
- 【cs229-Lecture4】GLMS:选定指数分布族,如何用它来推导出GLM?
在Lecture4中有3部分内容: Newton’s method 牛顿方法 Exceponential Family 指数分布族 Generalized Linear M ...
- 分布式实时日志系统(一)环境搭建之 Jstorm 集群搭建过程/Jstorm集群一键安装部署
最近公司业务数据量越来越大,以前的基于消息队列的日志系统越来越难以满足目前的业务量,表现为消息积压,日志延迟,日志存储日期过短,所以,我们开始着手要重新设计这块,业界已经有了比较成熟的流程,即基于流式 ...
- 【WEB前端系列之CSS】CSS3动画之Tranition
前言 css中的transition允许css的属性值在一定的时间区间内平滑的过渡.这种效果可以在鼠标点击.获得焦点.被点击或对元素任何改变中触发,并圆滑的以动画效果改变CSS的属性值.语法: tra ...
- css案例 - 评分效果的星星✨外衣
纳尼?什么星星外衣?好,直接上图比较能说清楚: 仔细看会发现规律:可以根据百分比/分值动态改变高亮星星的个数. 分步骤图: 这种效果,如果遇到一分一个星,没有半星(或者有也可以,直接加一个半星的类名) ...