// RootViewController.m
// UITabelView多行删除功能
//
// Created by 峰哥哥 on 15/5/19.
// Copyright (c) 2015年峰哥哥-.-. All rights reserved.
//
#import"RootViewController.h"
#import"StudentModel.h"
@interfaceRootViewController()
{
UITableView*_tbView;
//存储多选需要删除的数据
NSMutableArray*_deleteArray;
}
@property(nonatomic,strong)NSMutableArray*dataArray;
@end
@implementationRootViewController
- (void)viewDidLoad
{
[superviewDidLoad];
[selfcreateData];
self.automaticallyAdjustsScrollViewInsets=NO;
[selfcreateTableView];
//实现协议方法
//在导航栏添加删除按钮
UIBarButtonItem*item=[[UIBarButtonItemalloc]initWithTitle:@"删除"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(deleteData:)];
self.navigationItem.rightBarButtonItem=item;
/*初始化存储删除数组*/
_deleteArray=[NSMutableArraynew];
}
-(void)deleteData:(id)sender
{
//获取导航栏右侧的按钮的标题
NSString*title=self.navigationItem.rightBarButtonItem.title;
if([titleisEqualToString:@"删除"])
{
//进入表格的多选状态
_tbView.editing=YES;
//将导航栏的按钮改为确定
self.navigationItem.rightBarButtonItem.title=@"确定";
}
else
{
//执行删除操作
//--------将删除数组中的元素从数据源数组中删除- (void)removeObjectsInArray:(NSArray *)otherArray------------
[_dataArrayremoveObjectsInArray:_deleteArray];
//清空删除数组
[_deleteArrayremoveAllObjects];
//取消表格的编辑状态
_tbView.editing=NO;
//刷新表格
[_tbViewreloadData];
//将导航栏按钮的标题恢复成“删除”
self.navigationItem.rightBarButtonItem.title=@"删除";
}
}
//创建数据
-(void)createData
{
_dataArray=[NSMutableArraynew];
for(inti=0;i<30;i++)
{
StudentModel*model=[StudentModelnew];
model.name=[NSStringstringWithFormat:@"第%d个学生",i];
model.age=arc4random()%6+20;
[_dataArrayaddObject:model];
}
}
-(void)createTableView
{
//创建表格视图
_tbView=[[UITableViewalloc]initWithFrame:CGRectMake(0,64,self.view.frame.size.width,self.view.frame.size.height-64)style:UITableViewStylePlain];
_tbView.delegate=self;
_tbView.dataSource=self;
[self.viewaddSubview:_tbView];
}
#pragma mark-协议方法
//疑问:在reloadData时,会调用numbersOfRowsInSection方法吗?
//显示行数
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return_dataArray.count;
}
//显示cell内容
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
staticNSString*flag=@"reuse";
UITableViewCell*cell;
cell=[tableViewdequeueReusableCellWithIdentifier:flag];
if(!cell)
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:flag];
StudentModel*model=_dataArray[indexPath.row];
cell.textLabel.text=model.name;
cell.detailTextLabel.text=[NSStringstringWithFormat:@"年龄%d",model.age];
returncell;
}
//协议方法更改编辑状态
//默认时删除状态
-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath
{
//改为多选删除状态(位或)
//疑问:删除位或插入就可以返回多选删除吗?
returnUITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}
//选择某一行时讲将该行的内容添加到删除数组
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
//如果在编辑状态
if(_tbView.editing)
{
//取出选中的对象
StudentModel*model=_dataArray[indexPath.row];
//-----------如果删除数组不包含model------- (BOOL)containsObject:(id)anObject;-----
if(![_deleteArraycontainsObject:model])
{
//则添加到删除数组
[_deleteArrayaddObject:model];
}
}
}
//取消选择表示不需要删除这条数据,将该行的内容从删除数组中移除
-(void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath
{
//如果是编辑状态
if(_tbView.editing)
{
StudentModel*model=_dataArray[indexPath.row];
//如果删除数组包含这个对象
if([_deleteArraycontainsObject:model])
{
//则从删除数组中移除这个对象
[_deleteArrayremoveObject:model];
}
}
}
@end
//
// StudentModel.h
// UITabelView多行删除功能
//
// Created by 峰哥哥 on 15/5/19.
// Copyright (c) 2015年峰哥哥-.-. All rights reserved.
//
#import
@interfaceStudentModel :NSObject
@property(nonatomic,strong)NSString*name;
@property(nonatomic,assign)intage;
@end
//
// StudentModel.m
// UITabelView多行删除功能
//
// Created by 峰哥哥 on 15/5/19.
// Copyright (c) 2015年峰哥哥-.-. All rights reserved.
//
#import"StudentModel.h"
@implementationStudentModel
@end