UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,比如购物车、收藏列表等。
单行删除功能可以直接使用系统自带的删除功能,当横向轻扫cell时,右侧出现红色的删除按钮,点击删除当前cell。或者让表格进入编辑状态后,点击左侧的红色按钮,右侧出现删除按钮,删除,如下图所示。单行自带删除已经在前面文章中进行过讲解,需要的可以去查阅。
多选删除是点击编辑按钮,让表格进入编辑状态后,每行的左侧出现一个小圆圈,当点击行的时候,可以选中该行或者取消选中该行,当点击按钮确定删除的时候才会把选中的行全部删除掉,如图所示。
使用系统多选删除功能的步骤:
1、让tableView进入编辑状态,也就是设置它的editing为YES
2、返回编辑模式,也就是实现UITableViewDelegate中的tableview:editingStyleForRowAtIndexPath:方法,在里面返回UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert。如果不实现,默认返回的就是删除模式
3、实现UITableViewDelegate中的tableView: didSelectRowAtIndexPath: 和tableView: didDeselectRowAtIndexPath:方法。在里面对选中的商品集合中的数据进行修改
4、点击删除时,将选中商品数据从列表对应总商品集合中删除掉,并刷新界面。
代码:
// Goods.h// 购物车表格删除//// Created by jerei on 15-1-7.// Copyright (c) 2015年 jerei. All rights reserved.//#import@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//// Goods.m// 购物车表格删除//// Created by jerei on 15-1-7.// Copyright (c) 2015年 jerei. All rights reserved.//#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//// 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 () { UITableView *_tableView; //列表 NSMutableArray *_goodsAry; //商品数组 NSMutableArray *_selectArray; //选中的数组 UIButton *_editBtn; //编辑按钮}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //初始化选中数组 _selectArray = [NSMutableArray array]; //设置界面 [self setTheInterface]; //取数据 [self getGoodsInfoFromFile];}#pragma mark - 取数据-(void)getGoodsInfoFromFile{ NSArray *ary = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ShoppingGoodsList" ofType:@"plist"]]; //把数据存到模型对象中,然后把对象存到数组中 _goodsAry = [NSMutableArray array]; for (int i=0; i
作者: 出处: 版权声明:本文版权归和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 技术咨询: