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

 

代码如下:

 #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. Python的种类 Cpython     Python的官方版本,使用C语言实现,使用最为广泛,CPython实现会将源文件(py文件)转换成字节码文件(pyc文件),然后运行在Python虚 ...

  2. Gitlab-通过API管理项目

    Gitlab有一个非常强大的API,几乎可以通过API管理在Gitlab服务器中的所有项目. 在这里我们只是测试终端点的API, 因此我们需要一个程序来进行测试 .在这里我使用的是针对Google浏览 ...

  3. ubuntu 安装 theano

    参考博客: http://www.cnblogs.com/anyview/p/5025704.html 1. 安装gfortran, numpy, scipy, sklearn, blas, atla ...

  4. 《Android源码设计模式》--Builder模式

    No1: 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示 No2: 在Android源码中,最常用到的Builder模式就是AlertDialog.Builder No3: ...

  5. Adobe Audition CC 2017 (10.0)安装教程

    Adobe Audition CC 2017 (10.0)安装教程 注:下载地址在文章末尾 第一步:首先请将电脑的网络断开,很简单:禁用本地连接或者拔掉网线,这样就可以免除登录Creative Clo ...

  6. web服务端安全之SQL注入攻击

    一.SQL注入攻击的原理攻击者在HTTP请求中,注入恶意的SQL代码,并在服务端执行.比如用户登录,输入用户名camille,密码 ' or '1'='1 ,如果此时使用参数构造的方式,就会出现 ' ...

  7. kolla部署all-in-one环境(N版)

    简单介绍: Kolla 是 OpenStack 大帐篷模式下的官方子项目之一,其主要目标是通过利用 Docker 容器以及 Ansible 自动化部署工具,来为 OpenStack 云平台提 供一个简 ...

  8. JS (function (window, document, undefined) {})(window, document)的真正含义

    原文地址:What (function (window, document, undefined) {})(window, document); really means 按原文翻译 在这篇文章中,我 ...

  9. [HDU6155]Subsequence Count(线段树+矩阵)

    DP式很容易得到,发现是线性递推形式,于是可以矩阵加速.又由于是区间形式,所以用线段树维护. https://www.cnblogs.com/Miracevin/p/9124511.html 关键在于 ...

  10. hdu 5248 贪心

    题意: