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

最新文章

  1. Lesson 4 An existing trip
  2. 解决在VS2015下用C++开发的DLL在WIN7上无法加载运行
  3. Java 增强型的for循环 for each
  4. Oracle 中 sys和system帐号的区别
  5. JS中window.showModalDialog()详解
  6. MyBatis——实现关联表查询
  7. WebAPI 安全性 使用TOKEN+签名验证(下)
  8. 支付宝Demo 报错
  9. js 浮点数加减问题
  10. C++ Primer 学习笔记_45_STL实践与分析(19)--建筑常规算法
  11. HDU1425 &lt;sort 快排&gt;
  12. 算法笔记_017:递归执行顺序的探讨(Java)
  13. Linux计划任务crontab
  14. BZOJ4698 差分 + 二分 + SA
  15. yum安装命令:遇到的问题报错如下: File &quot;/usr/bin/yum&quot;, line 30 except KeyboardInterrupt, e: 通过看报错可以了解到是使用了python2的语法,所以了解到当前yum使用的Python2,因为我单独安装了python3,且python3设置为默认版本了,所以导致语法问题 解决方法: 使用python2.6 yum install
  16. vue全局 关键字搜索 v-search
  17. ios实例开发精品文章推荐(7.22)
  18. 51nod1671
  19. SharePoint2013使用资源管理器打开失败
  20. Spring boot 开发WebService遇到的问题之一

热门文章

  1. vars 变量预解析
  2. CodeForces 785B Anton and Classes
  3. 洛谷P1099 BZOJ1999 树网的核 [搜索,树的直径]
  4. set集合玩法、三目运算
  5. dp洋洋散散的知识+code
  6. python开发_搜索本地文件信息写入文件
  7. VK Cup 2016 - Qualification Round 2 D. Three-dimensional Turtle Super Computer 暴力
  8. 2015 UESTC 数据结构专题H题 秋实大哥打游戏 带权并查集
  9. paip.手机时间设置不能修改灰色禁用 解决大法
  10. Windows系统默认调试器设置