UITableView 的多选删除
2016-07-26 15:39
357 查看
// 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
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法