首页 > 代码库 > tableview 多选删除
tableview 多选删除
镔哥,就直接写了个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 {
[superviewDidLoad];
[self greatNav];
[self initData];
// Do any additional setup after loading the view, typically from a nib.
}
//导航栏设置
-(void)greatNav
{
self.view.backgroundColor = [UIColorwhiteColor];
self.navigationController.title =@"多选删除";
UINavigationBar *navbar = [[UINavigationBaralloc]initWithFrame:CGRectMake(0,0, 320, 64)];
[navbar setBackgroundColor:[UIColorclearColor]];
// 3.设置导航栏文字的主题
NSShadow *shadow=[[NSShadowalloc]init];
[shadow setShadowOffset:CGSizeMake(0,0)];
[navbar setTitleTextAttributes:@{
NSForegroundColorAttributeName : [UIColorblackColor],
NSShadowAttributeName : shadow
}];
// 设置状态栏样式
[UIApplicationsharedApplication].statusBarStyle =UIStatusBarStyleLightContent;
//创建一个导航栏集合
UINavigationItem *navItem = [[UINavigationItemalloc]initWithTitle:@"多选删除"];
//创建一个右边按钮
_rightBtn = [[UIBarButtonItemalloc] initWithTitle:@"删除"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(rightBtnPressed:)];
self.navigationItem.rightBarButtonItem =_rightBtn;
//把导航栏集合添加到导航栏中,设置动画关闭
[navbar pushNavigationItem:navItemanimated:NO];
[navItem setRightBarButtonItem:_rightBtn];
[self.viewaddSubview:navbar];
}
//初始化数据
-(void)initData
{
self.dataArray = [[NSMutableArrayalloc] initWithObjects:@"A",@"B", @"C",@"D", @"E",@"F", @"G",@"H", @"I",@"J", @"K",@"L", @"M",nil];
self.selectedDic = [[NSMutableArrayalloc] init];
tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,64, 320, self.view.frame.size.height-64)];
[self.viewaddSubview:tableView];
tableView.delegate=self;
tableView.dataSource =self;
}
- (IBAction)rightBtnPressed:(id)sender{
//显示多选圆圈
[tableViewsetEditing:YESanimated: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.selectedDiccount];
if (count > 0 ) {
for (int i =0; i < count; i++) {
NSInteger row = [[self.selectedDicobjectAtIndex:i] row];
[self.dataArrayremoveObjectAtIndex:row];
}
// NSLog(@"self.dataArray:------>:%@", self.dataArray);
[tableViewdeleteRowsAtIndexPaths:self.selectedDicwithRowAnimation:UITableViewRowAnimationFade];
[self.selectedDicremoveAllObjects];
// NSLog(@"self.selectedDic--------->:%@", self.selectedDic);
// [cloMableView reloadData];
_rightBtn.title =@"删除";
[_rightBtn setAction:@selector(rightBtnPressed:)];
[tableViewsetEditing:NOanimated:YES];
}else {
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"提示"message:@"未选中任何数据!"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:@"重新选择",nil];
[alert show];
}
}
#pragma -mark
#pragma tableview data source method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.dataArraycount];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
#pragma tableView delegate methods
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
returnUITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//添加一项
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([_rightBtn.titleisEqualToString:@"确定"]) {
[self.selectedDicaddObject:indexPath];
// NSLog(@"Select---->:%@",self.selectedDic);
}
}
//取消一项
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([_rightBtn.titleisEqualToString:@"确定"]) {
[self.selectedDicremoveObject: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 = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:tableViewIdentifier];
NSInteger row = [indexPath row];
cell.textLabel.text = [self.dataArrayobjectAtIndex: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:)];
[tableViewsetEditing:NOanimated:YES];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation !=UIInterfaceOrientationPortraitUpsideDown);
}
tableview 多选删除