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 ...
随机推荐
- php背景图片上生成二维码,二维码上带logo 代码示例 (原)
依赖库文件 phpqrcode.php (下载地址://www.jb51.net/codes/189897.html :或者在官网下载:http://phpqrcode.sourceforge.net ...
- 连接mysql && ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
上一篇:mysql服务正在启动 mysql服务无法启动 && mysql启动脚本 mysql关闭脚本 此篇目编写一个核心目的: 1.mysql连接 先抛出一个问题 这是因为mysql服 ...
- lnmp----------lnmp集成环境使用lnmp安装包安装lnmp集成环境的步骤
1.先看下screen -S lnmp 命令是否存在,不存在则安装.这个是个什么东东呢?百度一下( GNU Screen是一款由GNU计划开发的用于命令行终端切换的自由软件.用户可以通过该软件同时连接 ...
- python subprocess中ssh命令的特殊性
今天尝试使用python 的 subprocess 模块 使用类似如下语句: p=subprocess.Popen(['ssh','root@localhost'],stdout=subprocess ...
- 关于nginx的安装
今天倒腾nginx,当运用命令service nginx start 出现了这个:Job for nginx.service failed because the control process ex ...
- E. Gerald and Giant Chess
E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...
- 开源词袋模型DBow3原理&源码(二)ORB特征的保存和读取
util里提供了create_voc_step0用于批量生成features并保存,create_voc_step1读入features再生成聚类中心,比较适合大量语料库聚类中心的生成. 提取一张图的 ...
- 写自动更新程序出现"远程服务器返回错误: (404) 未找到"
在win2003配置后,在客户端运行时能够下载exe和dll文件,但是在更新lib文件时总是报“远程服务器返回错误: (404) 未找到”错误,不明白咋会出现这个问题,去网上一查,发现以下解决办法: ...
- 前端框架VUE----箭头函数
箭头函数 基本语法: ES6允许使用“箭头”(=>)定义函数 var f = a = > a //等同于 var f = function(a){ return a; } 如果箭头函数不需 ...
- 前端框架VUE----模板字符串
传统的JavaScript语言,输出模板通常是这样的写的. 1 $('#result').append( 2 'There are <b>' + basket.count + '</ ...