UITableView删除添加和移动
- #import "RootTableViewController.h"
- @interface RootTableViewController ()
- @property (nonatomic, strong) NSMutableArray *allDataArray;
- @property (nonatomic, assign) UITableViewCellEditingStyle style;
- @end
- @implementation RootTableViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor lightGrayColor];
- // 设置导航栏
- self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
- self.title = @"尹浩";
- self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
- [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:]}];
- // 处理数据
- [self handleData];
- // 注册tableView
- [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
- // 添加右按钮
- UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(rightBarButtonItemClick:)];
- UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(moveClick:)];
- self.navigationItem.rightBarButtonItems = @[button1, button2];
- // 添加左按钮
- self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftBarButtonItemClick:)];
- }
- // 处理数据
- - (void)handleData {
- // 1.初始化大数组
- self.allDataArray = [NSMutableArray array];
- // 2.定义三个数存放每一组学生的姓名
- NSMutableArray *array1 = @[@"泰隆", @"刀妹", @"卡牌大师", @"提莫", @"艾希", @"蛮王"].mutableCopy;
- NSMutableArray *array2 = @[@"盖聂", @"卫庄", @"天明", @"少羽", @"高月"].mutableCopy;
- NSMutableArray *array3 = @[@"尹浩", @"尹笑", @"尹双浩", @"尹冬冬", @"尹句号"].mutableCopy;
- // 3.将所有学生存放到大数组中
- [self.allDataArray addObject:array1];
- [self.allDataArray addObject:array2];
- [self.allDataArray addObject:array3];
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return self.allDataArray.count;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return [self.allDataArray[section] count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
- // 设置数据
- NSArray *array = [self.allDataArray objectAtIndex:indexPath.section];
- cell.textLabel.text = array[indexPath.row];
- return cell;
- }
- // 设置行高
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return ;
- }
- // 取消选中状态
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- #pragma mark - 编辑(删除和添加)
- // 右按钮点击事件
- - (void)rightBarButtonItemClick:(UIBarButtonItem *)sender {
- self.style = UITableViewCellEditingStyleDelete;
- // 让cell处于编辑状态
- [self.tableView setEditing:!self.tableView.editing animated:YES];
- }
- // 左按钮点击事件
- - (void)leftBarButtonItemClick:(UIBarButtonItem *)sender {
- self.style = UITableViewCellEditingStyleInsert;
- [self.tableView setEditing:!self.tableView.editing animated:YES];
- }
- // 指定哪些cell可以编辑
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.section == || indexPath.section == ) {
- return YES;
- }
- return NO;
- }
- // 设置编辑样式
- - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
- return self.style;
- }
- // 完成编辑
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
- if (editingStyle == UITableViewCellEditingStyleInsert) {
- [self.allDataArray[indexPath.section] insertObject:@"星魂" atIndex:indexPath.row + ];
- NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row + inSection:indexPath.section];
- [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
- } else if (editingStyle == UITableViewCellEditingStyleDelete) {
- [self.allDataArray[indexPath.section] removeObjectAtIndex:indexPath.row];
- [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
- }
- }
- #pragma mark - 移动
- // 让cell处于编辑状态
- - (void)moveClick:(UIBarButtonItem *)sender {
- [self.tableView setEditing:!self.tableView.editing animated:YES];
- }
- // 设置哪些cell可以移动
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
- return YES;
- }
- // 开始移动
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
- // 获取需要修改的数据
- NSString *sourceName = [self.allDataArray[sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];
- // 先将数据从当前位置移除
- [self.allDataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
- // 再将数据插入到对应的位置
- [self.allDataArray[destinationIndexPath.section] insertObject:sourceName atIndex:destinationIndexPath.row];
- }
- // 防止随意移动
- - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
- if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
- return proposedDestinationIndexPath;
- } else {
- return sourceIndexPath;
- }
- }
- @end
UITableView删除添加和移动的更多相关文章
- jquery删除添加输入文本框
效果体验:http://hovertree.com/texiao/jquery/67/ 效果图: 参考:http://hovertree.com/h/bjaf/traversing_each.htm ...
- lnmp一键安装包删除添加的域名
lnmp一键安装包删除添加的域名 如果使用lnmp一键安装包/root/vhost.sh 添加的域名可以,可以删除/usr/local/nginx/conf/vhost/要删除的域名.conf 文件, ...
- [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- cino伟斯 A770键盘界面快速设定记录后缀删除添加换行回车操作方法
http://www.cinoscan.com/upload/2016063033256485.pdf cino A770键盘界面快速设定记录后缀删除添加换行回车操作方法
- MySQL8.0数据库出现的问题——外码创建方式、外键约束两个引用列不兼容问题、check约束问题、用触发器代替check约束、关键字DELIMITER、删除添加索引、删除添加外键约束、和一些数据库方面的操作
一.首先先说一下我们都需要建立那些表 mysql> CREATE TABLE IF NOT EXISTS `student`( -> `sno` CHAR(8) NOT NULL, -&g ...
- JSP实现登录删除添加星座等(带样式)
功能要求 1.完成两个页面 2.第一个登陆页面login. jsp 3.第二个用户管理页面useManage. jsp 4.有登录功能(能进行用户名密码的校验,用户名若为自己的学号密码为班级号,允许登 ...
- UITableView的添加、删除、移动操作
#pragma mark -----表视图的移动操作----- //移动的第一步也是需要将表视图的编辑状态打开 //2.指定哪些行可以进行移动 - (BOOL)tableView:(UITableVi ...
- IOS UITableView删除功能
UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,比如购物车等.删除功能可以直接使用系统自带的删除功能,当横向轻扫cell时,右侧出现红色的删除按钮,点击删除当前cell. ...
- iOS-分组UITableView删除崩溃问题(当删除section中最后一条数据崩溃的情况)
错误: The number of sections contained in the table view after the update (1) must be equal to the num ...
随机推荐
- ArcGIS“一个或多个ActiveX控件无法显示...”问题的解决方案
ArcMap启动时的一个警告信息“一个或多个ActiveX控件无法显示...”,如图 出现这种情况,有可能的原因是IE浏览器的安全选项设置被修改了.比如被手动修改过,或者被第三方系统杀毒优化软件修改了 ...
- Spring Remoting: Hessian--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-hessian.jsp Concept Overview The p ...
- iOS-UIButton-设置button标题和图片位置
一.效果图 1.Button被点击之前 2.Button被点击之后 二.代码 - (void)createBtn3 { UIImage * buttonImage = [UIImage imageNa ...
- WCF回顾一、基本概念和应用场景
一.WCF描述 wcf是一款基于面向服务的架构的通讯框架平台,在分布式框架中得到了广泛使用. wcf入门非常简单,只要花几分钟就能编写一个完整的wcf程序,而实际上WCF是概念非常多的一门技术,需要花 ...
- 迭代接口的IEnumerator
我们经常在工作中用到对List,Dictionary对象的Foreach遍历,取出每一项. 其实这个接口很简单,只有一个属性2个方法. [ComVisible(true), Guid("49 ...
- C#130问,初级程序员的面试宝典
首先介绍下,目前C#作为一门快速开发的语言,在面试的过程中需要注意的技术知识点,了解下面的知识点对于初级工程师入职非常有帮助,也是自己的亲身体悟. 1. 简述 private. protecte ...
- C# winform调用浏览器打开页面方法分享,希望对大家有帮助
在很多客户端程序中我们都需要调用浏览器打开网页,这里分享一个可以在我winform程序调用浏览器的方法,测试通过了. 声明:这个方法是上万个用户测试通过的,不是我没有测试通过就拿出来分享,那个是自己搬 ...
- 老毛桃安装Win8(哪里不会点哪里,so easy)
先来一张美女图,是不是很漂亮呢!继续往下看! 英雄不问出路,美女不看岁数!求推荐啊! 每次碰到妹子找我装系统的时候我都毫不犹豫的答应了,心里暗暗想到:好好表现啊!此刻的心情比见家长还要激动和紧张! 有 ...
- Linux修改命令提示符(关于环境参量PS1)
关乎环境参量的四个文件/etc/profile /etc/bashrc ~/.bashrc ~/.bash_profile $$$:/etc/profile:此文件为系统的每个用户设置环境信息,当 ...
- 后缀数组---New Distinct Substrings
Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...