一般我们使用列表的形式展现数据就会用到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滑动删除的更多相关文章

  1. [转]ANDROID仿IOS微信滑动删除_SWIPELISTVIEW左滑删除例子

    转载:http://dwtedx.sinaapp.com/itshare_290.html 本例子实现了滑动删除ListView的Itemdemo的效果.大家都知道.这种创意是来源于IOS的.左滑删除 ...

  2. UITableViewCell滑动删除及移动

    实现Cell的滑动删除, 需要实现UITableView的代理UITableViewDelegate中如下方法: //先要设Cell可编辑 - (BOOL)tableView:(UITableView ...

  3. IOS tableView 滑动删除与排序功能

    // // ViewController.m // 0429 // // Created by apple on 15/4/29. // Copyright (c) 2015年 gense. All ...

  4. [Android-2A] -仿IOS微信滑动删除_SwipeListview左滑删除例子

    https://yunpan.cn/cueUIQkRafQrH (提取码:7ec1) 关于这样类似的例子网上的代码很多,最近发现这个例子里的代码在开发中会遇到一系列的问题.比如ListView的OnI ...

  5. IOS第13天(3,私人通讯录,登陆状态数据存储,数据缓存, cell的滑动删除,进入编辑模式,单个位置刷新 )

    *****联系人的界面的优化 HMContactsTableViewController.m #import "HMContactsTableViewController.h" # ...

  6. IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)

    **********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...

  7. android中列表的滑动删除仿ios滑动删除

    大家是不是觉得ios列表的滑动删除效果很酷炫?不用羡慕android也可以实现相同的效果 并且可以自定义效果,比如左滑删除,置顶,收藏,分享等等 其实就是自定义listview重写listview方法 ...

  8. IOS学习之路六(UITableView滑动删除指定行)

    滑动删除指定行代码如下: Controller.h文件 #import <UIKit/UIKit.h> @interface TableViewController : UIViewCon ...

  9. iOS边练边学--简单的数据操作(增、删、改),左滑动删除和弹窗

    一.数据刷新的原则: 通过修改模型数据,来修改tableView的展示 先修改数据模型 在调用数据刷新方法 不要直接修改cell上面子控件的属性 二.增删改用到的方法: <1>重新绑定屏幕 ...

随机推荐

  1. 用computed返回this.$store.state.count,store更改了,但是computed没有调用

    今天出现了这个问题,store更新了,你computed为啥不调用呢??? 另一个.vue更新了state,这个的computed就监听不到了么? 是用这种格式更新的this.$store.commi ...

  2. Java split()用法

    特殊情况有 * ^ : | . \ 一.单个符号作为分隔符  String address="上海\上海市|闵行区\吴中路"; String[] splitAddress=addr ...

  3. b/s 读取多个FTP文件(图片,视频)压缩到服务器 下载到客户端

    其实需求是这样, 要做一键导出, 有图片,有照片,youhtml,存在不同的文件夹,每次下载都必须下载最新数据,因为FTP是随时更新的. 1.这要是一直下载下载,浏览器一直跳窗口,蛋疼的我都看不下去. ...

  4. qq客服代码

    http://shang.qq.com/v3/widget.html <a target="_blank" href="http://wpa.qq.com/msgr ...

  5. Electron-使用Electron开发第一个应用

    使用Electron开发第一个应用 Electron 应用的目录结构如下: app/ ├── package.json ├── main.js └── index.html 新建一个app文件夹 将这 ...

  6. 【EasyUI】combotree和combobox模糊查询

    这里说的模糊查询指在输入框输入,然后自动在下拉框中显示匹配结果,类似Google搜索提示 EasyUI库已经实现了combobox的查询过滤功能,但只能从头匹配,原因是EasyUI库的代码限制: fi ...

  7. 异步编程模型(APM)模式

    什么是APM .net 1.0时期就提出的一种异步模式,并且基于IAsyncResult接口实现BeginXXX和EndXXX类似的方法. .net中有很多类实现了该模式(比如HttpWebReque ...

  8. Fastest Wordpress Theme -wpfast Download

    Are you in search of fastest wordpress theme to increase your product sell and conversion ,then you ...

  9. JS判断客户端是否是iOS或者Android

    通过判断浏览器的userAgent,用正则来判断是否是ios和Android客户端.代码如下: <script type="text/javascript"> var ...

  10. MySql中添加用户,新建数据库,用户授权,删除用户,修改密码

    1.新建用户 登录MYSQL: @>mysql -u root -p @>密码 创建用户: mysql> insert into mysql.user(Host,User,Passw ...