iOS:删除、插入、移动单元格
删除、插入、移动单元格的具体实例如下:



代码如下:
#import "ViewController.h"
#define NUM 20
typedef enum
{
deleteCell,
addCell,
moveCell,
}cellState;
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSMutableArray *arrayM;
@property (assign,nonatomic)cellState state; //对单元格要处理的状态
@end @implementation ViewController
//删除单元格
- (IBAction)addCellClicked:(UIBarButtonItem *)sender
{
self.state = addCell;
self.tableView.editing = !self.tableView.editing;
}
//插入单元格
- (IBAction)deleteCellClicked:(UIBarButtonItem *)sender
{
self.state = deleteCell;
self.tableView.editing = !self.tableView.editing;
}
//移动单元格
- (IBAction)moveCellClicked:(UIBarButtonItem *)sender
{
self.state = moveCell;
self.tableView.editing = !self.tableView.editing;
} - (void)viewDidLoad {
[super viewDidLoad]; //初始化
self.arrayM = [NSMutableArray arrayWithCapacity:NUM];
for(int i=; i<NUM; i++)
{
NSString *productName = [NSString stringWithFormat:@"product-%02d",i+];
[self.arrayM addObject:productName];
} //设置数据源和代理
self.tableView.dataSource = self;
self.tableView.delegate = self;
} #pragma mark -tableView的方法
//每一个section有多少个row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arrayM.count;
}
//设置每一个单元格的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
static NSString *reuseIdentifier = @"productCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
//2.如果没有找到,自己创建单元格对象
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
//3.设置单元格对象的内容
cell.textLabel.text = self.arrayM[indexPath.row];
return cell;
} #pragma mark -tableView的方法
//返回单元格编辑类型
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.state == deleteCell)
{
return UITableViewCellEditingStyleDelete; //删除
}
else if(self.state == addCell)
{
return UITableViewCellEditingStyleInsert; //插入
}
else
{
return UITableViewCellEditingStyleNone; //移动
}
}
//对单元格做编辑处理
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//取出当前的单元格
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if(cell.editingStyle == UITableViewCellEditingStyleDelete)//删除单元格
{
//1.删除该单元格
[self.arrayM removeObjectAtIndex:indexPath.row];
//2.局部刷新表格
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
if(cell.editingStyle == UITableViewCellEditingStyleInsert)//插入单元格
{
//1.创建产品资源
NSString *product = [NSString stringWithFormat:@"product-%02d",arc4random_uniform()]; //2.插入该数据到当前单元格
[self.arrayM insertObject:product atIndex:indexPath.row]; //3.整体刷新表格
[self.tableView reloadData];
}
}
//是否可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.state == deleteCell || self.state == addCell)
{
return NO;
}
else
{
return YES;
}
}
//移动单元格
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//修改数组中元素的顺序
//取出数组中要移动的元素
NSString *item = [self.arrayM objectAtIndex:sourceIndexPath.row]; //删除原来位置的元素
[self.arrayM removeObjectAtIndex:sourceIndexPath.row]; //在新的位置上重新插入新的元素
[self.arrayM insertObject:item atIndex:destinationIndexPath.row];
}
@end
iOS:删除、插入、移动单元格的更多相关文章
- Swift - 动态添加删除TableView的单元格(以及内部元件)
在Swift开发中,我们有时需要动态的添加或删除列表的单元格. 比如我们做一个消息提醒页面,默认页面只显示两个单元格.当点击第二个单元格(时间标签)时,下面会再添加一个单元格放置日期选择控件(同时新增 ...
- [Xcode 实际操作]五、使用表格-(10)插入UITableView单元格
目录:[Swift]Xcode实际操作 本文将演示如何插入一行单元格. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先添加两个协 ...
- IOS之表视图单元格删除、移动及插入
1.实现单元格的删除,实现效果如下 - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 self.editButtonItem.title = @&q ...
- 关于Aspose.Words插入表格单元格的高度问题的解决
最近在工作中遇到客户要将PDF打印的文档插入的表格行高缩小.为解决这个问题,我百度了好长时间,让没有直接来说明这个问题的,我不清楚是我遇到的问题太low了,各位大神不屑一顾.终于我在几个家之所长,把问 ...
- Java删除word合并单元格时的重复值
Spire.Doc提供了Table.applyVerticalMerge()方法来垂直合并word文档里面的表格单元格,Table.applyHorizontalMerge()方法来水平合并表格单元格 ...
- iOS UITableView 移除单元格选中时的高亮状态
在处理UITableView表格时,我们希望用户能够和触摸单元格式进行交互,但是希望用户在完成交互之后,这些单元格的选中状态能够消失,.Cocoa Touch 提供了两种方法来防止单元格背持久选中. ...
- iOS下UITableView的单元格重用逻辑
终于有时间继续UITableView的接口调用顺序这篇文章了,之前测试过,模拟器都是按照height,cellForRow这样的顺序调用接口的,iOS8以前一直是这样,但是到了iOS8,这个顺序倒过来 ...
- iOS中重用UITableView单元格时,千万别忘了这个
不多说,看截图
- iOS:多个单元格的删除(方法一)
采用存取indexPath的方式,来对多个选中的单元格进行删除 删除前: 删除后: 分析:如何实现删除多个单元格呢?这需要用到UITableView的代理方法,即选中单元格时对单元格做的处理,同时我们 ...
- EXCEL 将网络图片地址插入为锁定的图片单元格宏
Sub InsertPic2(ByVal 图片链接 As String, ByVal 插入图片表名 As String, ByVal 插入图片单元格地址 As String) On Error Res ...
随机推荐
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 C.二元-K个二元组最小值和最大-优先队列+贪心(思维)
链接:https://ac.nowcoder.com/acm/contest/558/C来源:牛客网 小猫在研究二元组. 小猫在研究最大值. 给定N个二元组(a1,b1),(a2,b2),…,(aN, ...
- C#语言-NPOI.dll导入Excel功能的实现
前言:刚工作那会,公司有一套完善的MVC框架体系,每当有导入EXCEL功能要实现的时候,都会借用框架里自带的导入方法,一般三下五除二就完成了,快是快,可总是稀里糊涂的,心里很没有底.前几天,在另一个原 ...
- 导出Excel超过65535条限制解决方案
使用poi导出excel的时候如果数据过多,超过65535条会报错,因为excel2003一个sheet表最多导出65535条,excel2007是10万4000多条限制. 因此遇到这种excel导出 ...
- Spring boot之SpringApplicationBuilder,@@Configuration注解,@Component注解
SpringApplicationBuilder: 该方法的作用是可以把项目打包成war包 需要配置启动类,pom.xml文件等,具体见:http://blog.csdn.net/linzhiqian ...
- CSS基础-DAY1
CSS 概述CSS 指层叠样式表 (Cascading Style Sheets),样式定义了如何显示 HTML文件中的标签元素,CSS是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标 ...
- jQuery中bind,live,delegate,on的区别
bind(),live()都是调用on()函数,只不过传递的参数不同. 一.$(selector).bind(event,data,fn); $('#J_btn').bind('click',func ...
- apache 监控
当前加载模块 [root@controller01 ~]# httpd -lCompiled in modules: core.c mod_so.c http_core.c 当前版本[root@con ...
- python sys.argv[]的用法简明解释
sys模块中文参考文档:http://xukaizijian.blog.163.com/blog/static/170433119201111625428624/ sys.argv[]: 「argv」 ...
- c++ 时间相关的类型
关于时间转换可以参考以下博客: https://www.jianshu.com/p/80de04b41c31 https://www.cnblogs.com/qicosmos/p/3642712.ht ...
- 【DFS好题】BZOJ1999- [Noip2007]Core树网的核(数据加强版)
NOIP的数据好水,一开始有好几个错结果NOIP数据就水过了?? [题目大意] 求无根树的直径上一段不超过S长的链,使得偏心距最小.具体概念见原题. [思路] 首先明确几个性质: (1)对于树中的任意 ...