UI一揽子计划 10 (UITableView 中cell 的编辑, 增加, 删除、UITableViewController 、)
* 1. 让tableView成为可编辑状态
* -1. 激活编辑状态
{
// 开启UITableView 编辑状态
// self.tableView.editing 默认是 NO 的
[self.tableView setEditing:!self.tableView.editing animated:YES];
// 更改按钮的标题
if (_tableView.editing) {
rightButton.title = @"完成";
} else {
rightButton.title = @"编辑";
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 先判断分区
if (indexPath.section == 0) {
// 再判断数据
if ([self.firstDataArray[indexPath.row] isEqualToString:@"添加"]) {
return UITableViewCellEditingStyleInsert;
}
} else if (indexPath.section == 1) {
// 再判断数据
if ([self.secondDataArray[indexPath.row] isEqualToString:@"添加"]) {
return UITableViewCellEditingStyleInsert;
}
}
return UITableViewCellEditingStyleDelete;
-1- √ 更新数据源数据
-2- √ 刷新界面
// 4. 按照编辑的样式 提交哪个分区哪行的结果 完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str = [NSString stringWithFormat:@"%d", i];
NSString *str1 = [NSString stringWithFormat:@"%d", j];
// 更新数据
// 刷新界面
// 先判断分区
if (indexPath.section == 0) {
// 第一分区
// 再判断编辑样式
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 删除
// 利用indexPath.row 删除数组中相对应的数据
[self.firstDataArray removeObjectAtIndex:indexPath.row];
// 刷新界面
// 下面方法 用在删除数据的时候刷新界面
// 需要一个数组 数组中是删除的索引 这个数组可以是多行的索引
// 下面的刷新方法是对行进行操作的
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
} else {
// 添加
// [self.firstDataArray insertObject:str atIndex:[self.firstDataArray count] - 1];
[self.firstDataArray insertObject:str atIndex:indexPath.row];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
i++;
}
} else {
// 第二分区
// 再判断编辑样式
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 删除
[self.secondDataArray removeObjectAtIndex:indexPath.row];
// 刷新界面
// 下面方法 用在删除数据的时候刷新界面
// 需要一个数组 数组中是删除的索引 这个数组可以是多行的索引
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
} else {
// 添加
[self.secondDataArray insertObject:str1 atIndex:indexPath.row];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationBottom)];
j++;
}
}
* 1. 让tableView成为可编辑状态
* 1. 激活编辑状态
*
*
*/
- (void)rightButton:(UIBarButtonItem *)rightButton
{
// 开启UITableView 编辑状态
// self.tableView.editing 默认是 NO 的
[self.tableView setEditing:!self.tableView.editing animated:YES];
// 更改按钮的标题
if (_tableView.editing) {
rightButton.title = @"完成";
} else {
rightButton.title = @"编辑";
}
{
return YES;
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
// sourceIndexPath 来源的索引 (拿起来cell 的位置)
// destinationIndexPath 目的地/终点的索引 (将来要放下cell 的位置)
{
// 分两种情况 1. 同section 的移动(同区移动), 不同section 之间的 移动(跨区移动)
if (sourceIndexPath.section == destinationIndexPath.section) {
// 同区
if (sourceIndexPath.section == 0) {
// 操作第一分区数组
// 先保存一下来源索引处的数据
NSString *str = self.firstDataArray[sourceIndexPath.row];
// 再从数组中 按来源索引删除该数据
[self.firstDataArray removeObjectAtIndex:sourceIndexPath.row];
// 最后再把保存的数据 插入到目的地的索引处
[self.firstDataArray insertObject:str atIndex:destinationIndexPath.row];
// 刷新页面
[self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
} else{
// 操作第二分区数组
NSString *str = self.secondDataArray[sourceIndexPath.row];
[self.secondDataArray removeObjectAtIndex:sourceIndexPath.row];
[self.secondDataArray insertObject:str atIndex:destinationIndexPath.row];
[self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
} else {
// 跨区
// 只要拖动 就会触发这个方法
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
// sourceIndexPath 来源的索引 (拿起来cell 的位置)
// proposedDestinationIndexPath 推荐/建议的 目的地/终点的索引 (将来要放下cell 的位置)
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
return proposedDestinationIndexPath;
} else {
return sourceIndexPath;
}
1. 自带了一个 self.tableView 根屏幕一样大
2. 代理协议 数据源 已经给写好了
UI一揽子计划 10 (UITableView 中cell 的编辑, 增加, 删除、UITableViewController 、)相关推荐
- iOS开发UI篇-在UItableview中实现加载更多功能
iOS开发UI篇-在UItableview中实现加载更多功能 一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据. 二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时 ...
- UI一揽子计划 21 (UICollectionView、SDWebImage第三方类库加载图片的使用、集合视图的布局UICollectionViewFlowLayout 、自定义Cell、布局协议
Pro : SDWebImage第三方类库加载图片的使用: 1.在MRC 环境下 使用ARC 的类库: -fobjc-arc (Build Phases --> Compile Sourc ...
- UI一揽子计划 9 (UITableView 、UITableView 、重用机制)
一. UITableView UITableView继承自UIScrollView,所以可以滚动 表视图的每⼀一条数据都是显示在UITableViewCell对象中 表视图可以分区显⽰示数据,每个分区 ...
- Repeater控件中如何做编辑和删除功能
做asp.net有六年了,Repeater控件使用了无数次,但每次都是只做显示. 今天这个需要编辑和删除功能,google了一下.Repeater需要加OnItemDataBound事件. OnIte ...
- UI一揽子计划 11 (自定义UITableViewCell、Cell 的自适应高度)
一. 自定义UITableViewCell 在日常的编程中,系统提供的几种Cell 样式 往往不能满足我们的需求.所以需要我们给它进行自定义样式. 自定义Cell 就是创建一个UITableViewC ...
- UI一揽子计划 1 (UIView UILabel)
第一天 1.UIView ·建立一个空模板后,首先将ARC模式关闭,因为iOS采用MRC模式,即手动管理内存. ·重写dealloc方法. - (void)dealloc { [_window rel ...
- UITableView中cell的圆角(第一个和最后一个)
#import <UIKit/UIKit.h> @interface SemicircleView : UIView /** * 传入数值(改变圆角位置) * * @param ...
- UI一揽子计划 14 (拖控件 、)
- (void)viewDidLoad { [super viewDidLoad]; // 注册cell // 如果使用Xib 文件加载cell 的话 一定要注 ...
- UI一揽子计划 2 (UITextField UIButton)
UItextField 一.创建一个UITextField时候默认就弹出键盘 1 对齐方式 textField.textAlignment = NSTextAlignmentCente ...
最新文章
- 有人能用外行人解释什么是JSONP吗? [重复]
- 向碳基芯片更进一步:台积电、斯坦福等联手开发碳纳米管晶体管新工艺,性能逼近硅元件...
- tensorflow r1.5 版本差异调研
- Tomcat的web项目部署方式
- 工作45:注意公用方法 别混入
- 剑指offer:12-17记录
- 【codevs2952】 细胞分裂2,快速幂模版
- 产品经理学习---人性七宗罪:打造完美产品的金钥匙
- CSS之position解释
- Java思维导图(3)
- VB2010实例(2) _滚动字幕
- UVA 1212 Duopoly
- 【ML学习笔记】2:机器学习中的数学基础2(琴生不等式,概率公式,统计量)
- Unity iOS包打出的app名称空格丢失
- 极客日报:苹果或推出粉色款iPhone 13;拼多多再超阿里
- 百度云视频 在线倍速播放
- 网站常见的攻击与防护
- 阿呆喵广告过滤 v1.9.0.1 官网版
- php刷新热销商品,ecshop首页如何调用指定分类下的推荐热销最新商品
- 【Algorithm】种子填充算法
热门文章
- 自然语言处理NLP星空智能对话机器人系列:业界动态信息分享 京东零售数据智能探索实践沙龙
- Google挖坑后人埋-ViewBinding(上)
- P4论文粗读笔记(一)
- PID控制算法学习与Matlab仿真
- Kubernetes 线下培训班
- c语言12之编程设计一个简单的计算器程序,要求根据用户从键盘输入的表达式:操作数1 运算符op 操作数2 计算表达式的值,指定的运算符为加减乘除。
- 电脑常用的快捷键Ctrl篇
- 学习linux第三步:给英文版的ubuntu18.04安装中文输入法
- This.invoke和this.begininvoke的区别?
- 【答学员问】如何提问问题