很多情況下應用需要批量處理功能,但UITableView並沒有類似的功能,但我們可以自己實現。
首先在UITableView的 edittingStyleForRowAtIndexPath函數中,添加如下代碼:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
returnUITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
這樣我們就可以得到下面的效果:
注意:初始時設置TableView setEditing=YES;
具體實現:
- //
- // 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
效果圖:
刪除後:
代碼比較簡單,就不過多解釋了!