因為镔哥學習iOS也不是很長時間,所以對很多控件都是一邊工作一邊學習,現在最近因為項目需求又研究了一下多選刪除,其實網上很多這樣的demo,但是基本不是純代碼,而且很多方面沒有考慮,然後我自己理解上又根基一些demo,自己先了一個,供大家一起學習。
我講解一下思路就直接代碼吧:
思路:一般要實現多選刪除
1:前提你要有數據:
NSMutableArray *dataArray;//臨時用假數據代替
2:你也要有一個存儲勾選刪除的數據NSMutableArray *removeList;//勾選時要刪除的數據
3:當你勾選刪除的時候,你要實現
[_dataArray removeObjectsInArray:self.removeList];//刪除已經勾選的數據
然後重新加載tableview
[tableView reloadData];//重新加載
接著就要清空已經勾選了的存儲數據
[self.removeList removeAllObjects];//清空已經勾選了的數據列表
4:你要實現tableView的代理方法
//編輯樣式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//添加一項
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//取消一項
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
//
// ViewController.m
// TableView多選刪除
//
// Created by apple on 14/12/9.
// Copyright (c) 2014年 huweibin. All rights reserved.
//
#import ViewController.h
@interface ViewController ()
{
UITableView *tableView;
}
@property(nonatomic,strong)NSMutableArray *dataArray;//臨時用假數據代替
@property (nonatomic, retain) NSMutableArray *removeList;//勾選時要刪除的數據
@property(nonatomic,strong)UIBarButtonItem *rightBtn;
@end
@implementation ViewController
@synthesize removeList;
- (void)viewDidLoad {
[super viewDidLoad];
[self initAllData];
}
//實現操作
-(void)initAllData
{
//1:必須首先加載tableview
tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, self.view.frame.size.height-64)];
[self.view addSubview:tableView];
tableView.delegate= self;
tableView.dataSource = self;
//2:初始化數據
_dataArray = [[NSMutableArray alloc] initWithObjects:@王菲,@周迅,@李冰冰,@白冰,@紫薇,@馬蘇,@劉詩詩,@趙薇,@angelbaby,@熊黛林,nil];
self.removeList = [[NSMutableArray alloc]init];
//3:導航欄設置
self.view.backgroundColor = [UIColor whiteColor];
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 orangeColor],
NSShadowAttributeName : shadow
}];
//4.創建一個導航欄集合
UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@多選刪除];
// 5.設置狀態欄樣式
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//6.創建一個右邊按鈕
_rightBtn = [[UIBarButtonItem alloc] initWithTitle:@刪除 style:UIBarButtonItemStyleDone target:self action:@selector(toggleEdit:)];
self.navigationItem.rightBarButtonItem = _rightBtn;
[navItem setRightBarButtonItem:_rightBtn];
//7.把導航欄集合添加到導航欄中,設置動畫關閉
[navbar pushNavigationItem:navItem animated:NO];
[self.view addSubview:navbar];
}
//多選刪除操作(其實很簡單,重點理解這裡)
-(void)toggleEdit:(id)sender {
[tableView setEditing:!tableView.editing animated:YES];//能出那個??勾選刪除
if (tableView.editing)
[self.navigationItem.rightBarButtonItem setTitle:@確定];
else
{
[self.navigationItem.rightBarButtonItem setTitle:@刪除];
if (self.removeList.count > 0) {
[_dataArray removeObjectsInArray:self.removeList];//刪除已經勾選的數據
[tableView reloadData];//重新加載
[self.removeList removeAllObjects];//清空已經勾選了的數據列表
}
}
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [_dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *DeleteMeCellIdentifier = @DeleteMeCellIdentifier;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
DeleteMeCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:DeleteMeCellIdentifier];
}
NSInteger row = [indexPath row];
cell.textLabel.text = [_dataArray objectAtIndex:row];
return cell;
}
#pragma mark -
#pragma mark Table View Data Source Methods
//編輯樣式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
//添加一項
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
id addObject = [_dataArray objectAtIndex:row];
[self.removeList addObject:addObject];
}
//取消一項
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
id deleteObject = [_dataArray objectAtIndex:row];
[self.removeList removeObject:deleteObject];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}