单元格的多选删除

 

 
#import "ViewController.h"
 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>@property(retain)UITableView* tableView;@property(retain)NSMutableArray* dataSource;@property(retain)NSMutableArray* removeData;@property(assign)BOOL isEdit;
@end
 
@implementation
ViewController
 
- (
void
)viewDidLoad {
    [
super viewDidLoad
];
   
self.view.backgroundColor=[UIColor cyanColor
];
   
//防止下滑
 
   
self.automaticallyAdjustsScrollViewInsets=NO
;
   
//申请内存
 
   
_dataSource=[NSMutableArray new
];
   
_removeData=[NSMutableArray new
];
   
//创建表格视图
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:UITableViewStylePlain];
    //添加   
   
   
//NSMutableArray* arr=[NSMutableArray new];
 
       
for (int j=0; j<15
; j++) {
          
               
NSString* str=[NSString stringWithFormat:@"男%d号"
,j];
               
//[arr addObject:str];
 
            [
_dataSource addObject
:str];
        }
       
//显示表格的编辑按钮
 
   
self.navigationItem.rightBarButtonItem=self.editButtonItem
;
   
//表格视图代理
 
   
_tableView.delegate=self
;
   
//数据源代理
 
   
_tableView.dataSource=self
;
    [
self.view addSubview:_tableView
];
 
}
//实现控制表格的编辑状态开启或者关闭的方法
 
-(
void)setEditing:(BOOL)editing animated:(BOOL
)animated
{
    [
super setEditing:editing animated
:animated];
   
//取非
 
   
_isEdit=!_isEdit
;
   
//开启表格的标记状态
 
    [
_tableView setEditing:_isEdit animated:YES
];
   
}
-(
UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath
{
   
return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert
;
}
-(
UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
   
//
 
   
static NSString* str=@"cell"
;
   
NSLog(@"单元格"
);
   
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier
:str];
   
//如果不存在就创建
 
   
if (cell==nil
) {
       
//创建单元格
 
        cell=[[
UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier
:str];
        }
   
//设置标题  因为标题在数据源中的小数组里面,所以必须先取到小数组  才能取到真正的标题内容  小数组的获取可以通过区的索引值来获取  标题可以通过行的索引值获取  也就是indexPath indexPath里面包含了当前组的索引值和当前行的索引值  indexPath.section 是当前组的索引值  indexPath.row 当前行的索引值
 
    cell.
textLabel.text=_dataSource[indexPath.row
];
   
      
return
cell;
}
 
-(
NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
)section
{
   
NSLog(@"行"
);
   
return [_dataSource count
];
}
//返回每个区尾部视图的高度
 
-(
CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger
)section
{
   
return 30
;
}
-(
UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger
)section
{
   
UIButton* btn=[UIButton buttonWithType:UIButtonTypeCustom
];
    btn.
frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30
);
    btn.
backgroundColor=[UIColor cyanColor
];
    [btn
addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside
];
    [btn
setTitle:@"Delete" forState:UIControlStateNormal
];
   
return
btn;
}
-(
void)btnAction:(UIButton
*)sender
{
   
NSLog(@" +++++"
);
   
//如果进入编辑状态才可以删除
 
   
if (_isEdit==YES
) {
       
//吧数据源内_removeData里的数据全部清空
 
        [
_dataSource removeObjectsInArray:_removeData
];
       
//清空上次选中行的数据
 
        [
_removeData removeAllObjects
];
       
//当表格的数据源发生变化时 reloadData  向表格发送消息  可以重新加载表格视图
 
        [
_tableView reloadData
];
    }
}
-(
void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath
*)indexPath
{
   
//先获取被选中行的数据
 
   
NSString* obj=[_dataSource objectAtIndex:indexPath.row
];
   
//存到 数组中
 
    [
_removeData addObject
:obj];
}
//当用户又一次点击某一行  则之前的行会调用这个方法(反选行操作)
 
-(
void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath
*)indexPath
{
   
//参数 indexPath 存放的就是上次被选中的行的信息
 
   
//移除上次选中该行进行 _removeData 中的信息
 
   
NSString* str=[_dataSource objectAtIndex:indexPath.row
];
   
if ([_removeData containsObject
:str]) {
        [
_removeData removeObject
:str];
    }
}
- (
void
)didReceiveMemoryWarning {
    [
super didReceiveMemoryWarning
];
   
// Dispose of any resources that can be recreated.
 
}
 
@end

---恢复内容结束---

 
#import "ViewController.h"
 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>@property(retain)UITableView* tableView;@property(retain)NSMutableArray* dataSource;@property(retain)NSMutableArray* removeData;@property(assign)BOOL isEdit;
@end
 
@implementation
ViewController
 
- (
void
)viewDidLoad {
    [
super viewDidLoad
];
   
self.view.backgroundColor=[UIColor cyanColor
];
   
//防止下滑
 
   
self.automaticallyAdjustsScrollViewInsets=NO
;
   
//申请内存
 
   
_dataSource=[NSMutableArray new
];
   
_removeData=[NSMutableArray new
];
   
//创建表格视图
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:UITableViewStylePlain];
    //添加   
   
   
//NSMutableArray* arr=[NSMutableArray new];
 
       
for (int j=0; j<15
; j++) {
          
               
NSString* str=[NSString stringWithFormat:@"男%d号"
,j];
               
//[arr addObject:str];
 
            [
_dataSource addObject
:str];
        }
       
//显示表格的编辑按钮
 
   
self.navigationItem.rightBarButtonItem=self.editButtonItem
;
   
//表格视图代理
 
   
_tableView.delegate=self
;
   
//数据源代理
 
   
_tableView.dataSource=self
;
    [
self.view addSubview:_tableView
];
 
}
//实现控制表格的编辑状态开启或者关闭的方法
 
-(
void)setEditing:(BOOL)editing animated:(BOOL
)animated
{
    [
super setEditing:editing animated
:animated];
   
//取非
 
   
_isEdit=!_isEdit
;
   
//开启表格的标记状态
 
    [
_tableView setEditing:_isEdit animated:YES
];
   
}
-(
UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath
{
   
return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert
;
}
-(
UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
   
//
 
   
static NSString* str=@"cell"
;
   
NSLog(@"单元格"
);
   
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier
:str];
   
//如果不存在就创建
 
   
if (cell==nil
) {
       
//创建单元格
 
        cell=[[
UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier
:str];
        }
   
//设置标题  因为标题在数据源中的小数组里面,所以必须先取到小数组  才能取到真正的标题内容  小数组的获取可以通过区的索引值来获取  标题可以通过行的索引值获取  也就是indexPath indexPath里面包含了当前组的索引值和当前行的索引值  indexPath.section 是当前组的索引值  indexPath.row 当前行的索引值
 
    cell.
textLabel.text=_dataSource[indexPath.row
];
   
      
return
cell;
}
 
-(
NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
)section
{
   
NSLog(@"行"
);
   
return [_dataSource count
];
}
//返回每个区尾部视图的高度
 
-(
CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger
)section
{
   
return 30
;
}
-(
UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger
)section
{
   
UIButton* btn=[UIButton buttonWithType:UIButtonTypeCustom
];
    btn.
frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30
);
    btn.
backgroundColor=[UIColor cyanColor
];
    [btn
addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside
];
    [btn
setTitle:@"Delete" forState:UIControlStateNormal
];
   
return
btn;
}
-(
void)btnAction:(UIButton
*)sender
{
   
NSLog(@" +++++"
);
   
//如果进入编辑状态才可以删除
 
   
if (_isEdit==YES
) {
       
//吧数据源内_removeData里的数据全部清空
 
        [
_dataSource removeObjectsInArray:_removeData
];
       
//清空上次选中行的数据
 
        [
_removeData removeAllObjects
];
       
//当表格的数据源发生变化时 reloadData  向表格发送消息  可以重新加载表格视图
 
        [
_tableView reloadData
];
    }
}
-(
void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath
*)indexPath
{
   
//先获取被选中行的数据
 
   
NSString* obj=[_dataSource objectAtIndex:indexPath.row
];
   
//存到 数组中
 
    [
_removeData addObject
:obj];
}
//当用户又一次点击某一行  则之前的行会调用这个方法(反选行操作)
 
-(
void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath
*)indexPath
{
   
//参数 indexPath 存放的就是上次被选中的行的信息
 
   
//移除上次选中该行进行 _removeData 中的信息
 
   
NSString* str=[_dataSource objectAtIndex:indexPath.row
];
   
if ([_removeData containsObject
:str]) {
        [
_removeData removeObject
:str];
    }
}
- (
void
)didReceiveMemoryWarning {
    [
super didReceiveMemoryWarning
];
   
// Dispose of any resources that can be recreated.
 
}
 
@end
posted @ 2017-07-31 15:52  LongYP1  阅读(291)  评论(0编辑  收藏  举报