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>重新绑定屏幕 ...
随机推荐
- Linux下面CentOS 7桌面的安装
搭建本地yum仓库的方法 http://www.cnblogs.com/lql123/p/5952788.html 1.yum grouplist #列出yum仓库里的软件组列表 2.yum grou ...
- ListView 的使用
一.ListView:不同视图中一种显示想的集合,显示表格样式的数据信息1.视图: 在其右上方小箭头点击将视图改为Details:或者右键属性在外观View将其改为Details ...
- HTML 列表 <ol><ul><li><dl><dt><dd>
<ol>标签-有序列表 定义和用法: <ol>标签定义有序列表. HTML 与 XHTML 之间的差异 在 HTML 4.01 中,ol 元素的 "compact&q ...
- 繁星——JQuery选择器之层级
[ancestor descendant] 在给定元素下匹配所有后代元素.这个选择器的使用概率相当之高,使用示例如下: //HTML代码: <div id='div01'> <inp ...
- 【 2013 Multi-University Training Contest 2 】
HDU 4611 Balls Rearrangement 令lcm=LCM(a,b),gcd=GCD(a,b).cal(n,a,b)表示sum(abs(i%a-i%b)),0<=i<n. ...
- Android中的dp, px, pt
定义: px是像素,表示屏幕显示的最小元素单位 pt是磅数,一磅等于1/72英寸,一般用来作为字体的单位使用 问题: px和pt不使用于手机,因为同样的px在高低分辨率的手机上显示的比例不同 解决办法 ...
- ado.net中,数据的批量处理
//btBigImport按钮点击事件 private void btBigImport_Click(object sender, RoutedEventArgs e) { //连接字符 ...
- EF实体框架数据操作接口(转)
//----------------------------------------------------------------// Copyright (C) 2013 河南禄恒软件科技有限公司 ...
- 【转】关于Oracle将小于1的数字to_char后丢掉0的解决办法
SQL代码如下: select rtrim(to_char(0.11, 'fm9990.99'), '.') from dual; 其中0.11为需要to_char的数字fm去掉字符串前面的空格999 ...
- 解决maven创建web项目卡死在generator插件(转)
如下图所示:在Properties中添加一个参数archetypeCatalog=internal,不加这个参数,在maven生成骨架的时候将会非常慢,有时候会直接卡住. 理由 archetypeCa ...