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 ...
随机推荐
- Django实战(22):处理登录和注销
我们已经可以在view函数中判断用户是否已经登录以及获取用户信息: if request.user.is_authenticated(): #判断用户是否已登录 user = request.user ...
- ffmpeg 添加filter步骤
a). 自己写一个XXX.c文件,比如vf_transform.c,放在libavfilter目录下.代码可以参考其他filter: b) 在libavfilter/allfilters.c添加一行: ...
- hdoj2955 Robberies(01背包)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2955 题意 有n家银行,每家银行有两个属性:钱数m,概率p,p表示抢这家银行被逮着的概率.有一个人想抢 ...
- 谷歌pagerank算法简介
在这篇博客中我们讨论一下谷歌pagerank算法.这是参考的原博客连接:http://blog.jobbole.com/71431/ PageRank的Page可是认为是网页,表示网页排名,也可以认为 ...
- Collabtive 系统 SQL 注入实验(补充)
SQL Injection:就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 具体来说,它是利用现有应用程序,将(恶意)的SQL命令注 ...
- 网络请求+Gson解析--Retrofit 2
其实内部是封装了Okhttp和Gson解析 public class CourseFragmentAPI { public static void get(String userId, BaseCal ...
- Python并发编程-IO模型-IO多路复用实现SocketServer
Server.py import select import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.setblockin ...
- react篇章-React Props
state 和 props 主要的区别在于 props 是不可变的,而 state 可以根据与用户交互来改变.这就是为什么有些容器组件需要定义 state 来更新和修改数据. 而子组件只能通过 pro ...
- HTTP请求的GET与POST方式的区别
Form中的get和post方法,在数据传输过程中分别对应了HTTP协议中的GET和POST方法.二者主要区别如下: 1)Get是用来从服务器上获得数据,而Post是用来向服务器上传递数据: 2)Ge ...
- 01-学前入门.Net两种交换模式
C/S:客户机(Client)/服务器模式(Server)Winfrom应用程序 B/S:浏览器(Browser)/服务器模式(Server)Internet应用模式