UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,比如购物车等。删除功能可以直接使用系统自带的删除功能,当横向轻扫cell时,右侧出现红色的删除按钮,点击删除当前cell。

  使用系统自带删除功能的步骤:

1、让tableView进入编辑状态,也就是设置它的editing为YES

2、返回编辑模式,也就是实现UITableViewDelegate中的tableview:editingStyleForRowAtIndexPath:方法,在里面返回删除模式。如果不实现,默认返回的就是删除模式

3、提交删除操作,也就是实现tableview:commitEditingStyle:editing StyleForRowAtIndexPath:方法。只要实现此方法,就默认实现了系统横扫出现删除按钮的删除方法

4、如果想把删除按钮改为中文,可以实现tableView:titleForDeleteConfirmationButtonForRowAtIndexPath方法

代码:

//  ViewController.m
// JRTableView删除
//
// Created by jerehedu on 15/6/11.
// Copyright (c) 2015年 jerehedu. All rights reserved.
// #import "ViewController.h"
#import "Goods.h" @interface ViewController ()<UITableViewDataSource, UITableViewDelegate> {
UITableView *_tableView; //列表 NSMutableArray *_goodsAry; //商品数组 UIButton *_editBtn; //编辑按钮
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; //添加标题
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, )];
titleLabel.text = @"购物车";
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.backgroundColor = [UIColor redColor];
titleLabel.textColor = [UIColor whiteColor];
[self.view addSubview:titleLabel]; //添加编辑按钮
_editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_editBtn.frame = CGRectMake(self.view.frame.size.width-, , , );
[_editBtn setTitle:@"编辑" forState:UIControlStateNormal];
[_editBtn setTitle:@"完成" forState:UIControlStateSelected];
_editBtn.titleLabel.font = [UIFont systemFontOfSize:];
_editBtn.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.5];
[self.view addSubview:_editBtn];
[_editBtn addTarget:self action:@selector(clickEditBtn:) forControlEvents:UIControlEventTouchUpInside]; //添加tableview
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height-)];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView]; //取数据
NSArray *ary = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ShoppingGoodsList" ofType:@"plist"]]; //把数据存到模型对象中,然后把对象存到数组中
_goodsAry = [NSMutableArray array];
for (int i=; i<ary.count; i++) {
Goods *good = [Goods goodsWithDic:ary[i]];
[_goodsAry addObject:good];
}
}
#pragma mark 数据源 返回有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _goodsAry.count;
} #pragma mark 每行显示内容
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *idGood = @"goods"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idGood]; if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:idGood];
} Goods *good = _goodsAry[indexPath.row]; cell.imageView.image = [UIImage imageNamed:good.icon];
cell.textLabel.text = good.name;
cell.detailTextLabel.text = good.details;
cell.detailTextLabel.numberOfLines = ;
cell.detailTextLabel.textColor = [UIColor brownColor]; return cell;
} #pragma mark 选中行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//取消选中状态
[tableView deselectRowAtIndexPath:indexPath animated:YES];
} #pragma mark 设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
} #pragma mark 点击编辑按钮
- (IBAction)clickEditBtn:(UIButton *)sender { //设置tableview编辑状态
BOOL flag = !_tableView.editing;
[_tableView setEditing:flag animated:YES];
_editBtn.selected = flag; } #pragma mark 返回编辑模式,默认为删除模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// return UITableViewCellEditingStyleNone;
// return UITableViewCellEditingStyleInsert;
return UITableViewCellEditingStyleDelete;
} #pragma mark 提交编辑操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//只要实现这个方法,就实现了默认滑动删除!!!!!
if (editingStyle != UITableViewCellEditingStyleDelete)
return; //删除数据模型
[_goodsAry removeObjectAtIndex:indexPath.row]; //刷新界面
// [_tableView reloadData]; [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} #pragma mark 删除按钮中文
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
} @end //
// Goods.h
// 购物车表格删除
//
// Created by jerei on 15-1-7.
// Copyright (c) 2015年 jerei. All rights reserved.
// #import <Foundation/Foundation.h> @interface Goods : NSObject @property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *details; -(id)initWithDic:(NSDictionary*)dic;
+(id)goodsWithDic:(NSDictionary*)dic; @end #import "Goods.h" @implementation Goods -(id)initWithDic:(NSDictionary *)dic
{
if (self = [super init]) {
self.icon = [dic objectForKey:@"icon"];
self.name = [dic objectForKey:@"name"];
self.details = [dic objectForKey:@"details"];
}
return self;
} +(id)goodsWithDic:(NSDictionary *)dic
{
Goods *good = [[Goods alloc] initWithDic:dic];
return good;
} @end

  疑问咨询或技术交流,请加入官方QQ群: (452379712)

作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 

IOS UITableView删除功能的更多相关文章

  1. iOS UITableView删除cell分割线

    UITableView是UITableViewStylePlain风格的,这样整个TableView都会被分割线分隔开,不管有没有数据,非常丑. 为了可以自定义cell的分割线: 解决方案: 将UIT ...

  2. iOS - UITableView 单选功能实现

    #import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property(nonatomic,copy)NSStrin ...

  3. IOS UITableView多选删除功能

    UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,比如购物车.收藏列表等. 单行删除功能可以直接使用系统自带的删除功能,当横向轻扫cell时,右侧出现红色的删除按钮,点击删除 ...

  4. iOS UITableView划动删除的实现

    标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...

  5. MUI - H5实现ios长按图标后进入图标排序及删除功能的效果

    html5实现ios长按图标后进入图标排序及删除功能的效果 我们知道在ios(国产定制安卓系统基本都有)设备上按下图标,图标就会不停的抖动,并且可以随心拖动排序和删除. 那么问题来了,我们怎么通过ht ...

  6. IOS UITableView NSIndexPath属性讲解

    IOS UITableView NSIndexPath属性讲解   查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和deleg ...

  7. tableView左滑删除功能

    实现三个代理方法即可 -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtI ...

  8. tableView删除功能小记

    由于项目需要,做一个UITableView来实现删除功能. 效果如图: 功能思路其实不难: 交代一下,我自己要实现的效果: 1.TableView是分组的. 2.点击删除按钮后,某行被删除.   写完 ...

  9. iOS-分组UITableView删除崩溃问题(当删除section中最后一条数据崩溃的情况)

    错误: The number of sections contained in the table view after the update (1) must be equal to the num ...

随机推荐

  1. 【WPF】OnApplyTemplate

    操作模板控件 在做WPF开发的时候,我们通常因为满足不同的需求会开发一些自定义控件来满足需要,我们会自定义模板来定义控件的外观,添加命令和路由事件来给控件添加行为,那如何在模板中查找元素并关联事件处理 ...

  2. 2018IEEE冬季生物识别学校 5天课程

    里边有很多介绍及相关报告的PPT https://www.comp.hkbu.edu.hk/wsb18/index.php https://www.comp.hkbu.edu.hk/wsb18/pro ...

  3. POJ 1741 树分治

    题目链接[http://poj.org/problem?id=1741] 题意: 给出一颗树,然后寻找点对(u,v)&&dis[u][v] < k的对数. 题解: 这是一个很经典 ...

  4. 【BZOJ 2749】 2749: [HAOI2012]外星人 (数论-线性筛?类积性函数)

    2749: [HAOI2012]外星人 Description Input Output 输出test行,每行一个整数,表示答案. Sample Input 1 2 2 2 3 1 Sample Ou ...

  5. android 同一个service启动之后 能不能被绑定bind

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 可以 startService 启动了一个服务,这个服务可以再调用 bindServic ...

  6. java 继承 String类

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha String 类 是 final修饰 , 是不能 继承的.

  7. luoguP4206 [NOI2005]聪聪与可可 期望概率DP

    首先,分析一下这个猫和鼠 猫每局都可以追老鼠一步或者两步,但是除了最后的一步,肯定走两步快些.... 既然猫走的步数总是比老鼠多,那么它们的距离在逐渐缩小(如果这题只能走一步反而不能做了...) 猫不 ...

  8. [SDOI2017]数字表格 --- 套路反演

    [SDOI2017]数字表格 由于使用markdown的关系 我无法很好的掌控格式,见谅 对于这么简单的一道题竟然能在洛谷混到黑,我感到无语 \[\begin{align*} \prod\limits ...

  9. HDU1251 统计难题 trie树 简单

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 找前缀数量 裸模板 #include<cstdio> #include<cstr ...

  10. Android Studio --> Gradle Build设置自动

    ps:http://www.cnblogs.com/kangyi/p/4448398.html 应用场景 通常情况下我们的apps发布后也就是release模式下log是不显示的,debug模式下是显 ...