镔哥,就直接写了个demo用来做参考:
//
// ViewController.h
// TableView多选删除
//
// Created by apple on 14/12/9.
// Copyright (c) 2014年 youdianshang. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *tableView;
}
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *selectedDic;
@property(nonatomic,strong)UIBarButtonItem *rightBtn;
@end
//
// ViewController.m
// TableView多选删除
//
// Created by apple on 14/12/9.
// Copyright (c) 2014年 youdianshang. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize dataArray;
@synthesize selectedDic;
- (void)viewDidLoad {
[super viewDidLoad];
[self greatNav];
[self initData];
// Do any additional setup after loading the view, typically from a nib.
}
//导航栏设置
-(void)greatNav
{
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.title = @"多选删除";
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 64)];
[navbar setBackgroundColor:[UIColor clearColor]];
// 3.设置导航栏文字的主题
NSShadow *shadow=[[NSShadow alloc]init];
[shadow setShadowOffset:CGSizeMake(0, 0)];
[navbar setTitleTextAttributes:@{
NSForegroundColorAttributeName : [UIColor blackColor],
NSShadowAttributeName : shadow
}];
// 设置状态栏样式
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//创建一个导航栏集合
UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@"多选删除"];
//创建一个右边按钮
_rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleDone target:self action:@selector(rightBtnPressed:)];
self.navigationItem.rightBarButtonItem = _rightBtn;
//把导航栏集合添加到导航栏中,设置动画关闭
[navbar pushNavigationItem:navItem animated:NO];
[navItem setRightBarButtonItem:_rightBtn];
[self.view addSubview:navbar];
}
//初始化数据
-(void)initData
{
self.dataArray = [[NSMutableArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", nil];
self.selectedDic = [[NSMutableArray alloc] init];
tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, self.view.frame.size.height-64)];
[self.view addSubview:tableView];
tableView.delegate= self;
tableView.dataSource = self;
}
- (IBAction)rightBtnPressed:(id)sender{
//显示多选圆圈
[tableView 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);
[tableView deleteRowsAtIndexPaths:self.selectedDic withRowAnimation:UITableViewRowAnimationFade];
[self.selectedDic removeAllObjects];
// NSLog(@"self.selectedDic--------->:%@", self.selectedDic);
// [cloMableView reloadData];
_rightBtn.title = @"删除";
[_rightBtn setAction:@selector(rightBtnPressed:)];
[tableView setEditing:NO animated:YES];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未选中任何数据!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"重新选择", nil];
[alert show];
}
}
#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:)];
[tableView setEditing:NO animated:YES];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
原文地址:http://blog.csdn.net/sammyieveo/article/details/41827787