iphone开发之实现UITableView多选删除功能详解
2013-09-13 23:00
441 查看
很多情况下应用需要批量处理功能,但UITableView并没有类似的功能,但我们可以自己实现。
首先在UITableView的 edittingStyleForRowAtIndexPath函数中,添加如下代码:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
returnUITableViewCellEditingStyleDelete |
UITableViewCellEditingStyleInsert;
}
这样我们就可以得到下面的效果:
注意:初始时设置TableView setEditing=YES;
具体实现:
[cpp]
view plaincopyprint?
//
// CloViewController.m
// MuTableViewTest
//
// Created by Cloay on 12-6-26.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "CloViewController.h"
@interface CloViewController ()
@end
@implementation CloViewController
@synthesize dataArray;
@synthesize selectedDic;
- (IBAction)rightBtnPressed:(id)sender{
//显示多选圆圈
[cloMableView setEditing:YES animated:YES];
rightBtn.title = @"确定";
[rightBtn setAction:@selector(rightBtnPressedWithSure:)];
}
- (IBAction)rightBtnPressedWithSure:(id)sender{
//do something with selected cells like delete
// NSLog(@"selectedDic------->:%@", self.selectedDic);
int count = [self.selectedDic count];
if (count > 0 ) {
for (int i = 0; i < count; i++) {
NSInteger row = [[self.selectedDic objectAtIndex:i] row];
[self.dataArray removeObjectAtIndex:row];
}
// NSLog(@"self.dataArray:------>:%@", self.dataArray);
[cloMableView deleteRowsAtIndexPaths:self.selectedDic withRowAnimation:UITableViewRowAnimationFade];
[self.selectedDic removeAllObjects];
// NSLog(@"self.selectedDic--------->:%@", self.selectedDic);
// [cloMableView reloadData];
rightBtn.title = @"删除";
[rightBtn setAction:@selector(rightBtnPressed:)];
[cloMableView setEditing:NO animated:YES];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未选中任何数据!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"重新选择", nil];
[alert show];
[alert release];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.dataArray = [[NSMutableArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", nil];
self.selectedDic = [[NSMutableArray alloc] init];
rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(rightBtnPressed:)];
self.navigationItem.rightBarButtonItem = rightBtn;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma -mark
#pragma tableview data source method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.dataArray count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
#pragma tableView delegate methods
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//添加一项
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([rightBtn.title isEqualToString:@"确定"]) {
[self.selectedDic addObject:indexPath];
// NSLog(@"Select---->:%@",self.selectedDic);
}
}
//取消一项
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([rightBtn.title isEqualToString:@"确定"]) {
[self.selectedDic removeObject:indexPath];
// NSLog(@"Deselect---->:%@",self.selectedDic);
}
}
//选择后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableViewIdentifier = @"TableViewIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewIdentifier];
NSInteger row = [indexPath row];
cell.textLabel.text = [self.dataArray objectAtIndex:row];
return cell;
}
#pragma mark-
#pragma AlertView delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
rightBtn.title = @"删除";
[rightBtn setAction:@selector(rightBtnPressed:)];
[cloMableView setEditing:NO animated:YES];
}
}
@end
效果图:
删除后:
代码比较简单,就不过多解释了!
有问题请留言,大家一起交流学习!
说明:转载请注明出处!http://blog.csdn.net/cloay/article/details/7697180
首先在UITableView的 edittingStyleForRowAtIndexPath函数中,添加如下代码:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
returnUITableViewCellEditingStyleDelete |
UITableViewCellEditingStyleInsert;
}
这样我们就可以得到下面的效果:
注意:初始时设置TableView setEditing=YES;
具体实现:
[cpp]
view plaincopyprint?
//
// CloViewController.m
// MuTableViewTest
//
// Created by Cloay on 12-6-26.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "CloViewController.h"
@interface CloViewController ()
@end
@implementation CloViewController
@synthesize dataArray;
@synthesize selectedDic;
- (IBAction)rightBtnPressed:(id)sender{
//显示多选圆圈
[cloMableView setEditing:YES animated:YES];
rightBtn.title = @"确定";
[rightBtn setAction:@selector(rightBtnPressedWithSure:)];
}
- (IBAction)rightBtnPressedWithSure:(id)sender{
//do something with selected cells like delete
// NSLog(@"selectedDic------->:%@", self.selectedDic);
int count = [self.selectedDic count];
if (count > 0 ) {
for (int i = 0; i < count; i++) {
NSInteger row = [[self.selectedDic objectAtIndex:i] row];
[self.dataArray removeObjectAtIndex:row];
}
// NSLog(@"self.dataArray:------>:%@", self.dataArray);
[cloMableView deleteRowsAtIndexPaths:self.selectedDic withRowAnimation:UITableViewRowAnimationFade];
[self.selectedDic removeAllObjects];
// NSLog(@"self.selectedDic--------->:%@", self.selectedDic);
// [cloMableView reloadData];
rightBtn.title = @"删除";
[rightBtn setAction:@selector(rightBtnPressed:)];
[cloMableView setEditing:NO animated:YES];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未选中任何数据!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"重新选择", nil];
[alert show];
[alert release];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.dataArray = [[NSMutableArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", nil];
self.selectedDic = [[NSMutableArray alloc] init];
rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(rightBtnPressed:)];
self.navigationItem.rightBarButtonItem = rightBtn;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma -mark
#pragma tableview data source method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.dataArray count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
#pragma tableView delegate methods
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//添加一项
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([rightBtn.title isEqualToString:@"确定"]) {
[self.selectedDic addObject:indexPath];
// NSLog(@"Select---->:%@",self.selectedDic);
}
}
//取消一项
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([rightBtn.title isEqualToString:@"确定"]) {
[self.selectedDic removeObject:indexPath];
// NSLog(@"Deselect---->:%@",self.selectedDic);
}
}
//选择后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableViewIdentifier = @"TableViewIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewIdentifier];
NSInteger row = [indexPath row];
cell.textLabel.text = [self.dataArray objectAtIndex:row];
return cell;
}
#pragma mark-
#pragma AlertView delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
rightBtn.title = @"删除";
[rightBtn setAction:@selector(rightBtnPressed:)];
[cloMableView setEditing:NO animated:YES];
}
}
@end
// // CloViewController.m // MuTableViewTest // // Created by Cloay on 12-6-26. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "CloViewController.h" @interface CloViewController () @end @implementation CloViewController @synthesize dataArray; @synthesize selectedDic; - (IBAction)rightBtnPressed:(id)sender{ //显示多选圆圈 [cloMableView setEditing:YES animated:YES]; rightBtn.title = @"确定"; [rightBtn setAction:@selector(rightBtnPressedWithSure:)]; } - (IBAction)rightBtnPressedWithSure:(id)sender{ //do something with selected cells like delete // NSLog(@"selectedDic------->:%@", self.selectedDic); int count = [self.selectedDic count]; if (count > 0 ) { for (int i = 0; i < count; i++) { NSInteger row = [[self.selectedDic objectAtIndex:i] row]; [self.dataArray removeObjectAtIndex:row]; } // NSLog(@"self.dataArray:------>:%@", self.dataArray); [cloMableView deleteRowsAtIndexPaths:self.selectedDic withRowAnimation:UITableViewRowAnimationFade]; [self.selectedDic removeAllObjects]; // NSLog(@"self.selectedDic--------->:%@", self.selectedDic); // [cloMableView reloadData]; rightBtn.title = @"删除"; [rightBtn setAction:@selector(rightBtnPressed:)]; [cloMableView setEditing:NO animated:YES]; }else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未选中任何数据!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"重新选择", nil]; [alert show]; [alert release]; } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.dataArray = [[NSMutableArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", nil]; self.selectedDic = [[NSMutableArray alloc] init]; rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(rightBtnPressed:)]; self.navigationItem.rightBarButtonItem = rightBtn; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } #pragma -mark #pragma tableview data source method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.dataArray count]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } #pragma tableView delegate methods - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } //添加一项 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if ([rightBtn.title isEqualToString:@"确定"]) { [self.selectedDic addObject:indexPath]; // NSLog(@"Select---->:%@",self.selectedDic); } } //取消一项 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ if ([rightBtn.title isEqualToString:@"确定"]) { [self.selectedDic removeObject:indexPath]; // NSLog(@"Deselect---->:%@",self.selectedDic); } } //选择后 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ // } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *tableViewIdentifier = @"TableViewIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewIdentifier]; cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewIdentifier]; NSInteger row = [indexPath row]; cell.textLabel.text = [self.dataArray objectAtIndex:row]; return cell; } #pragma mark- #pragma AlertView delegate method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 0) { rightBtn.title = @"删除"; [rightBtn setAction:@selector(rightBtnPressed:)]; [cloMableView setEditing:NO animated:YES]; } } @end
效果图:
删除后:
代码比较简单,就不过多解释了!
有问题请留言,大家一起交流学习!
说明:转载请注明出处!http://blog.csdn.net/cloay/article/details/7697180
相关文章推荐
- iphone开发之实现UITableView多选删除功能详解
- iphone开发之实现UITableView多选删除功能详解
- iPhone开发之实现UITableView多选删除功能详解
- iPhone开发之实现UITableView多选删除功能详解
- iOS开发:UITableview的多选功能实现
- UITableView类似Email的多选删除功能实现
- iOS自定义UITableView实现不同系统下的左滑删除功能详解
- iOS开发:UITableView实现侧滑删除cell的功能
- iOS开发中实现UITableView的Cell左划删除等自定义功能
- iOS项目开发小技巧 (三) --UITableView实现Cell左划删除等自定义功能
- 详解iOS开发中UItableview控件的数据刷新功能的实现
- ios系列开发之UITableView实现多行删除
- Android Recyclerview实现多选,单选,全选,反选,批量删除的功能
- iPhone开发之UITableView的小试牛刀 利用MVC模式从plist文件读取数据并字典转模型的格式实现
- iOS开发UI篇—在UItableview中实现加载更多功能
- iOS 开发-实现TableView的编辑功能以删除数据
- UIday1002:UITableView 2 用UITableView实现通讯录cell简单的数据移动、删除、添加功能
- Android开发——使用高级的RecyclerView实现侧滑菜单删除功能(SwipeRecyclerView)
- iOS UITableView实现滑动删除、增加、多选
- Android开发实现的ViewPager引导页功能(动态加载指示器)详解