iOS开发 -------- UITableView的编辑
一 核心API
Class: UITableView
Delegate: UITableViewDataSource, UITableViewDelegate
涉及到的API:
插入和删除 1 /**
* 让tableView进入或退出编辑状态(TableView方法)
*/
- (void)setEditing:(BOOL)editing animated:(BOOL)animated; /**
* 指定哪些行的cell可以进行编辑(UITableViewDataSource协议中方法)
*/
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; /**
* 指定cell的编辑状态(删除还是插入)(UITableViewDataSource协议中方法)
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath; /**
* 选中删除(或插入)状态之后的操作(数据源数组进行更新,cell删除或插入)(UITableViewDataSource协议中方法)
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; /**
* 插入cell(UITableView方法)
*/
- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; /**
* 删除cell(UITableView方法)
*/
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 移动
/**
* 移动cell后的操作: 数据源进行更新
*/
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath; /**
* 指定tableView哪些行可以移动
*/
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
二 功能实现
(1) 让TableView进入编辑状态
(2) 指定那些行cell可以进行编辑
(3) 指定cell的编辑状态(删除还是插入)
(4) 选中删除(或插入)状态之后的操作(数据源进行更新,cell删除或插入)
(5) 移动cell后的操作: 数据源进行更新
三 代码实现
//
// TableViewController.m
// UITableViewEdit
//
// Created by lovestarfish on 15/11/11.
// Copyright © 2015年 S&G. All rights reserved.
// #import "TableViewController.h"
#import "UIImage+Scale.h" @interface TableViewController ()
//数据源数组
@property (nonatomic,strong) NSMutableArray *dataSource; @end @implementation TableViewController /**
* 懒加载为数据源数组开辟空间
*/
- (NSMutableArray *)dataSource {
if (!_dataSource) {
self.dataSource = [NSMutableArray array];
}
return _dataSource;
} - (void)viewDidLoad {
[super viewDidLoad];
//添加系统的编辑按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//读取本地数据
[self readDataFromLocal];
} /**
* 从本地plist文件读取数据
*/
- (void)readDataFromLocal {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
self.dataSource = [NSMutableArray arrayWithArray:dic[@"app"]];
} #pragma mark - Table view data source
/**
* 返回分区数目
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ;
} /**
* 返回分区的行数
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.count;
} /**
* 返回分区的cell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
//为cell上的控件赋值
cell.imageView.image = [[UIImage imageNamed:self.dataSource[indexPath.row][@"image"]] scaleToSize:CGSizeMake(, )];
cell.textLabel.text = self.dataSource[indexPath.row][@"name"]; return cell;
} /**
* 让tableView进入或退出编辑状态(TableView方法)
*/
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
//先调用父类的方法
[super setEditing:editing animated:animated]; //使tableView处于编辑状态
[self.tableView setEditing:editing animated:animated];
} /**
* 指定哪些行的cell可以进行编辑(UITableViewDataSource协议中方法)
*/
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if ( == indexPath.row) {
return NO; //第一行不能进行编辑
} else {
return YES;
}
} #pragma mark 插入&&删除
/**
* 指定cell的编辑状态(删除还是插入)(UITableViewDataSource协议中方法)
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
//不同行,可以设置不同的编辑样式,编辑样式是一个枚举类型
if ( == indexPath.row) {
return UITableViewCellEditingStyleInsert; //插入
} else {
return UITableViewCellEditingStyleDelete; //删除
}
} /**
* 选中删除(或插入)状态之后的操作(数据源数组进行更新,cell删除或插入)(UITableViewDataSource协议中方法)
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//点击 删除 按钮的操作
if (editingStyle == UITableViewCellEditingStyleDelete) {//判断编辑状态为删除时 //1. 更新数据源数组: 根据indexPath.row作为数组下标,从数组中删除数据
[self.dataSource removeObjectAtIndex:indexPath.row]; //2. tableView中 删除一个cell
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} //点击 + 号的操作
if (editingStyle == UITableViewCellEditingStyleInsert) {//判断编辑状态为插入时 //1. 更新数据源: 向数组中添加数据
NSDictionary *dic = @{@"name":@"微信",@"image":@"微信"};
[self.dataSource insertObject:dic atIndex:indexPath.row]; //2. tableView中插入一个cell
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
} #pragma mark 移动
/**
* 移动cell后的操作: 数据源进行更新
*/
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
//1. 从原位置移除,在原位置移除之前,需要保存一下原位置的数据
NSDictionary *dic = self.dataSource[fromIndexPath.row];
[self.dataSource removeObjectAtIndex:fromIndexPath.row]; //2. 添加到目的位置
[self.dataSource insertObject:dic atIndex:toIndexPath.row];
} /**
* 指定tableView哪些行可以移动
*/
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
if ( == indexPath.row) {
return NO; //NO cell不能移动
} else {
return YES; //YES cell可以移动
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} @end
iOS开发 -------- UITableView的编辑的更多相关文章
- Xamarin iOS开发中的编辑、连接、运行
Xamarin iOS开发中的编辑.连接.运行 创建好工程后,就可以单击Xamarin Studio上方的运行按钮,如图1.37所示,对HelloWorld项目进行编辑.连接以及运行了.运行效果如图1 ...
- iOS开发UITableView基本使用方法总结
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- iOS开发,UITableView相关问题
第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.co ...
- iOS开发-UITableView自定义Cell
UITableView在iOS中开发的重要地位是毋庸置疑的,基本上应用中用到的比例是一半左右,而且大部分情况都是需要自定义单元格的,这样用户看到的App才能更有美感.之前写过UITableView的基 ...
- iOS开发UITableView的动画cell
1.动画cell 针对cell的动画,在Delegate中对cell的layer进行操作: 2.实现代码 #import "ViewController.h" #import &q ...
- iOS开发--UITableView
-.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; [Data ...
- iOS开发UITableView基本使用方法总结1
UITableView基本使用方法 1.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource 2.然后 ...
- iOS开发 UITableView之cell
1.cell简介 UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行 U ...
随机推荐
- 在window下搭建Vue.Js开发环境
nodejs官网http://nodejs.cn/下载安装包,无特殊要求可本地傻瓜式安装,这里选择2017-5-2发布的 v6.10.3,也可选择阿里云镜像https://npm.taobao.org ...
- 32.js 判断当前页面是否被浏览
可以通过document.hidden属性判断当前页面是否是激活状态. 兼容性:IE10+,Firefox10+,Chrome14+,Opera12.1+,Safari7.1+ 兼容性写法示例: va ...
- mysql----------阿里云RDS导入导出
1.这是阿里云rds如何将导出的物理备份文件,导入到自建库里面: https://help.aliyun.com/knowledge_detail/5973700.html?spm=5176.7766 ...
- Python全栈-day11-函数3
装饰器 1.开放封闭原则 通常情况下,软件一旦上线就应该遵循开放封闭原则,即对修改封闭.对扩展开放 扩展开放需遵循两个原则: 1)不修改源代码 2)不修改原函数的调用方式 2.装饰器 器指的是工具,装 ...
- Rpgmakermv(32) Yep_mainmenumanager
============================================================================ Introduction ========== ...
- hdu5489 树状数组+dp
2015-10-06 21:49:54 这题说的是个给了一个数组,然后删除任意起点的一个连续的L个数,然后求最长递增子序列<是递增,不是非递减>,用一个树状数组维护一下就ok了 #incl ...
- 【2017-02-28】C# 冒泡排序
冒泡排序 重复地走访过要排序的数列,一次比较两个元素的大小,如果他们的顺序错误就把他们交换过来 通过两个For循环嵌套来实现 思路——以从小到大为例 第一个for循环抽取第一个数和第二个数进行比较,如 ...
- canvas添加水印
<canvas id="canvas"></canvas><canvas id="water"></canvas> ...
- Spring源码阅读(四)
我们知道,在spring bean生命周期中,我们可以在不同阶段执行处理器或者方法,比如init-method,destroy方法,BeanPostProcessor接口等.那么这些处理器或方法的执行 ...
- 20165305 苏振龙《Java程序设计》第三周学习总结
面向对象和面向过程: 面向对象是相对面向过程而言的,面向过程强调的是功能行为,面向对象是将过程封装进对象,强调具备了功能的对象,面向对象是基于面向过程的. 面向对象的三个特征: 封装,继承,多态: 对 ...