iOS UITableViewCell滑动删除
一般我们使用列表的形式展现数据就会用到UITableView.在熟练掌握了用UITableView展示数据以后,开发过程中可能会遇到需要删除数据的需求,我们想实现在一行数据上划动一下,然后出现一个删除按钮的效果,其实只需要实现UITableView的一些代理方法就可以了。
首先,我们初始化一个界面,以列表的形式展示:
#pragma mark - 初始化UI
- (void)initUI{
self.view.backgroundColor = RGB(242, 242, 247);
self.automaticallyAdjustsScrollViewInsets = NO;
sideslipTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, kScreenWidth, kScreenHeight - 60) style:UITableViewStylePlain];
sideslipTableView.backgroundColor = [UIColor clearColor];
sideslipTableView.delegate = self;
sideslipTableView.dataSource = self;
sideslipTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:sideslipTableView];
}
然后,准备数据源:
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
UITableView *sideslipTableView;
//可变数组,用于删除数据
NSMutableArray *dataArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI];
dataArray = [NSMutableArray arrayWithArray: @[@"1111",@"2222",@"3333",@"4444",@"5555",@"6666",@"7777",@"8888",@"9999"]];
}
接下来我们要将数据显示出来:
#pragma mark - 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return dataArray.count;
}
#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 46;
}
#pragma mark - cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *indefier = @"cell";
sideslipTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indefier];
if (!cell) {
cell = [[sideslipTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indefier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.lable.text = dataArray[indexPath.row];
return cell;
}
最后,实现UITableView的一些代理方法:
//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
//进入编辑模式,按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[dataArray removeObjectAtIndex:indexPath.row];
// Delete the row from the data source.
[sideslipTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
}
这样就可以实现UITableViewCell滑动删除的效果啦。
iOS UITableViewCell滑动删除的更多相关文章
- [转]ANDROID仿IOS微信滑动删除_SWIPELISTVIEW左滑删除例子
转载:http://dwtedx.sinaapp.com/itshare_290.html 本例子实现了滑动删除ListView的Itemdemo的效果.大家都知道.这种创意是来源于IOS的.左滑删除 ...
- UITableViewCell滑动删除及移动
实现Cell的滑动删除, 需要实现UITableView的代理UITableViewDelegate中如下方法: //先要设Cell可编辑 - (BOOL)tableView:(UITableView ...
- IOS tableView 滑动删除与排序功能
// // ViewController.m // 0429 // // Created by apple on 15/4/29. // Copyright (c) 2015年 gense. All ...
- [Android-2A] -仿IOS微信滑动删除_SwipeListview左滑删除例子
https://yunpan.cn/cueUIQkRafQrH (提取码:7ec1) 关于这样类似的例子网上的代码很多,最近发现这个例子里的代码在开发中会遇到一系列的问题.比如ListView的OnI ...
- IOS第13天(3,私人通讯录,登陆状态数据存储,数据缓存, cell的滑动删除,进入编辑模式,单个位置刷新 )
*****联系人的界面的优化 HMContactsTableViewController.m #import "HMContactsTableViewController.h" # ...
- IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)
**********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...
- android中列表的滑动删除仿ios滑动删除
大家是不是觉得ios列表的滑动删除效果很酷炫?不用羡慕android也可以实现相同的效果 并且可以自定义效果,比如左滑删除,置顶,收藏,分享等等 其实就是自定义listview重写listview方法 ...
- IOS学习之路六(UITableView滑动删除指定行)
滑动删除指定行代码如下: Controller.h文件 #import <UIKit/UIKit.h> @interface TableViewController : UIViewCon ...
- iOS边练边学--简单的数据操作(增、删、改),左滑动删除和弹窗
一.数据刷新的原则: 通过修改模型数据,来修改tableView的展示 先修改数据模型 在调用数据刷新方法 不要直接修改cell上面子控件的属性 二.增删改用到的方法: <1>重新绑定屏幕 ...
随机推荐
- angularJs|es6|reactJs|vueJs相关技术(请访问https://expendo.github.io/)
技术博客地址:https://expendo.github.io/
- 听着好像很牛的特效——幽灵按钮DOM
给大家分享一个听着好像很牛的东西——幽灵按钮,这个玩意对于艺术设计细胞在高中决定不在考试试卷上画画的我来说,实在不感冒.但是这个按钮的设计元素很流行,一个网页东西不做几个,光放上几个按钮就会显得很高端 ...
- Android开发--Android Studio配置
1.常见问题 emulator: You might want to adjust your AVD RAM size and/or HAXM configuration to run in fast ...
- ios基础篇(二十七)—— Json解析
一.什么是Json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使 ...
- C# Bitmap 复制
以后再详述,先上代码. public bool CopyBitmap(Bitmap source, Bitmap destination) { if ((source.Width != destina ...
- react-native执行 npm install cl.exe找不到 的问题
最近在学习react-native,昨天在尝试某个demo时,执行 npm instal, 总是遇到 cl.exe文件找不到,最开始以为Microsoft Visual C++ 2015 Redist ...
- HashMap底层实现原理/HashMap与HashTable区别/HashMap与HashSet区别
①HashMap的工作原理 HashMap基于hashing原理,我们通过put()和get()方法储存和获取对象.当我们将键值对传递给put()方法时,它调用键对象的hashCode()方法来计算h ...
- Ubuntu导入证书
(*.cer) 首先我们得进入JAVA_HOME目录(查看/etc/profile文件),然后进去以下路径/opt/jdk1.5.0_11/jre/lib/security/ 将证书放进去,假如说是1 ...
- ittun配置和使用教程
有好久没写博了.时间过得真快,一下子一年又过去了. 不墨迹这么多了. 作为一个前端,页面写好了少不了做测试以及给其他同事看看效果,然而公司没有测试服务器,真是悲催哦. 一直都是用ip地址看页面的.但是 ...
- JavaScript 开发的45个经典技巧
JavaScript是一个绝冠全球的编程语言,可用于Web开发.移动应用开发(PhoneGap.Appcelerator).服务器端开发(Node.js和Wakanda)等等.JavaScript还是 ...