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

 

代码如下:

 #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:删除、插入、移动单元格的更多相关文章

  1. Swift - 动态添加删除TableView的单元格(以及内部元件)

    在Swift开发中,我们有时需要动态的添加或删除列表的单元格. 比如我们做一个消息提醒页面,默认页面只显示两个单元格.当点击第二个单元格(时间标签)时,下面会再添加一个单元格放置日期选择控件(同时新增 ...

  2. [Xcode 实际操作]五、使用表格-(10)插入UITableView单元格

    目录:[Swift]Xcode实际操作 本文将演示如何插入一行单元格. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先添加两个协 ...

  3. IOS之表视图单元格删除、移动及插入

    1.实现单元格的删除,实现效果如下 - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 self.editButtonItem.title = @&q ...

  4. 关于Aspose.Words插入表格单元格的高度问题的解决

    最近在工作中遇到客户要将PDF打印的文档插入的表格行高缩小.为解决这个问题,我百度了好长时间,让没有直接来说明这个问题的,我不清楚是我遇到的问题太low了,各位大神不屑一顾.终于我在几个家之所长,把问 ...

  5. Java删除word合并单元格时的重复值

    Spire.Doc提供了Table.applyVerticalMerge()方法来垂直合并word文档里面的表格单元格,Table.applyHorizontalMerge()方法来水平合并表格单元格 ...

  6. iOS UITableView 移除单元格选中时的高亮状态

    在处理UITableView表格时,我们希望用户能够和触摸单元格式进行交互,但是希望用户在完成交互之后,这些单元格的选中状态能够消失,.Cocoa Touch 提供了两种方法来防止单元格背持久选中. ...

  7. iOS下UITableView的单元格重用逻辑

    终于有时间继续UITableView的接口调用顺序这篇文章了,之前测试过,模拟器都是按照height,cellForRow这样的顺序调用接口的,iOS8以前一直是这样,但是到了iOS8,这个顺序倒过来 ...

  8. iOS中重用UITableView单元格时,千万别忘了这个

    不多说,看截图

  9. iOS:多个单元格的删除(方法一)

    采用存取indexPath的方式,来对多个选中的单元格进行删除 删除前: 删除后: 分析:如何实现删除多个单元格呢?这需要用到UITableView的代理方法,即选中单元格时对单元格做的处理,同时我们 ...

  10. EXCEL 将网络图片地址插入为锁定的图片单元格宏

    Sub InsertPic2(ByVal 图片链接 As String, ByVal 插入图片表名 As String, ByVal 插入图片单元格地址 As String) On Error Res ...

随机推荐

  1. python日常总结

    1. post请求中是否可以在url中携带请求体信息? 可以.Get请求时,请求体放在URL中; POST请求,请求体既可以是Form表单中的数据 也可以在请求的URL地址中放请求体信息. 如: &l ...

  2. Ubuntu16.04下HBase的安装与配置

    一.环境 os : Ubuntu 16.04 LTS 64bit jdk : 1.8.0_161 hadoop : mysql : hive : hbase: -hadoop2 安装HBase前,系统 ...

  3. Ionic实战五:ionic图表源码基于highcharts

    ionic图表demo基于Highcharts,highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程序添加有交互性的图表.HighCha ...

  4. 移动端h5下ul实现横向滚动css代码

    html代码: <ul id="category"> <li>品牌团</li> <li>美体个护</li> <li ...

  5. java面试 关键字

    1. final关键字有哪些用法? 修饰类.方法和变量. (1) final变量是只读的,不允许改变其引用,与static共用可声明常量.JVM会对final变量进行优化,比如常量折叠. (2) fi ...

  6. BZOJ3996 TJOI2015线性代数

    先把矩阵式子化简 原式=∑i=1n∑j=1nA[i]∗B[i][j]∗A[j]−∑i=1nA[i]∗C[i] 因此我们发现问题转化为选取一个点所获收益是B[i][j],代价是C[i][j] 这是一个最 ...

  7. 【spfa】【动态规划】zoj3847 Collect Chars

    转载自:http://blog.csdn.net/madaidao/article/details/42616743 Collect Chars Time Limit: 2 Seconds       ...

  8. Codeforces Round #357 (Div. 2) E. Runaway to a Shadow 计算几何

    E. Runaway to a Shadow 题目连接: http://www.codeforces.com/contest/681/problem/E Description Dima is liv ...

  9. URAL 1877 Bicycle Codes

    1877. Bicycle Codes Time limit: 0.5 secondMemory limit: 64 MB Den has two four-digit combination loc ...

  10. 《学习OpenCv》 笔记(1)

    P1-P17 废话 可跳过 不过讲了如何搭建环境,如果你没有搭建的话,可以查看我的另外一个博文,详细讲了如何构建OpenCv的编程环境 P19 开始编写第一个代码