iPhone开发之实现UITableView多选删除功能详解

来源:互联网 发布:阿凡达妹妹唱功知乎 编辑:程序博客网 时间:2024/04/30 09:35

很多情况下应用需要批量处理功能,但UITableView并没有类似的功能,但我们可以自己实现。

首先在UITableView的 edittingStyleForRowAtIndexPath函数中,添加如下代码:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    returnUITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;

}

这样我们就可以得到下面的效果:



注意:初始时设置TableView setEditing=YES;

具体实现:

  1. //   
  2. //  CloViewController.m   
  3. //  MuTableViewTest   
  4. //   
  5. //  Created by Cloay on 12-6-26.   
  6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.   
  7. //   
  8.   
  9. #import "CloViewController.h"   
  10.   
  11. @interface CloViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation CloViewController  
  16. @synthesize dataArray;  
  17. @synthesize selectedDic;  
  18.   
  19. - (IBAction)rightBtnPressed:(id)sender{  
  20.     //显示多选圆圈   
  21.     [cloMableView setEditing:YES animated:YES];  
  22.     rightBtn.title = @"确定";  
  23.     [rightBtn setAction:@selector(rightBtnPressedWithSure:)];  
  24. }  
  25.   
  26. - (IBAction)rightBtnPressedWithSure:(id)sender{  
  27.     //do something with selected cells like delete   
  28. //    NSLog(@"selectedDic------->:%@", self.selectedDic);   
  29.     int count = [self.selectedDic count];  
  30.     if (count > 0 ) {  
  31.         for (int i = 0; i < count; i++) {  
  32.             NSInteger row = [[self.selectedDic objectAtIndex:i] row];  
  33.             [self.dataArray removeObjectAtIndex:row];  
  34.         }  
  35.         //    NSLog(@"self.dataArray:------>:%@", self.dataArray);   
  36.         [cloMableView deleteRowsAtIndexPaths:self.selectedDic withRowAnimation:UITableViewRowAnimationFade];  
  37.         [self.selectedDic removeAllObjects];  
  38.         //    NSLog(@"self.selectedDic--------->:%@", self.selectedDic);   
  39. //        [cloMableView reloadData];   
  40.         rightBtn.title = @"删除";  
  41.         [rightBtn setAction:@selector(rightBtnPressed:)];  
  42.         [cloMableView setEditing:NO animated:YES];  
  43.     }else {  
  44.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未选中任何数据!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"重新选择", nil];  
  45.         [alert show];  
  46.         [alert release];  
  47.     }  
  48. }  
  49.   
  50. - (void)viewDidLoad  
  51. {  
  52.     [super viewDidLoad];  
  53.     // Do any additional setup after loading the view, typically from a nib.   
  54.     self.dataArray = [[NSMutableArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", nil];  
  55.     self.selectedDic = [[NSMutableArray alloc] init];  
  56.       
  57.     rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(rightBtnPressed:)];  
  58.     self.navigationItem.rightBarButtonItem = rightBtn;  
  59. }  
  60.   
  61. - (void)viewDidUnload  
  62. {  
  63.     [super viewDidUnload];  
  64.     // Release any retained subviews of the main view.   
  65. }  
  66.   
  67. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  68. {  
  69.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  70. }  
  71.   
  72. #pragma -mark   
  73. #pragma tableview data source method   
  74. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  75.     return  [self.dataArray count];  
  76. }  
  77.   
  78. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  79.     return 1;  
  80. }  
  81. #pragma tableView delegate methods   
  82. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{  
  83.     return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;  
  84. }  
  85.   
  86. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{  
  87.     return YES;  
  88. }  
  89. //添加一项   
  90. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  91.     if ([rightBtn.title isEqualToString:@"确定"]) {  
  92.         [self.selectedDic addObject:indexPath];  
  93. //        NSLog(@"Select---->:%@",self.selectedDic);   
  94.     }  
  95. }  
  96.   
  97. //取消一项   
  98. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{  
  99.     if ([rightBtn.title isEqualToString:@"确定"]) {  
  100.         [self.selectedDic removeObject:indexPath];  
  101. //        NSLog(@"Deselect---->:%@",self.selectedDic);   
  102.     }  
  103. }  
  104.   
  105. //选择后   
  106. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{  
  107.     //   
  108. }  
  109.   
  110. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  111.     static NSString *tableViewIdentifier = @"TableViewIdentifier";  
  112.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewIdentifier];  
  113.     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewIdentifier];  
  114.     NSInteger row = [indexPath row];  
  115.     cell.textLabel.text = [self.dataArray objectAtIndex:row];  
  116.     return cell;  
  117. }  
  118.   
  119. #pragma mark-   
  120. #pragma AlertView delegate method   
  121. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{  
  122.     if (buttonIndex == 0) {  
  123.         rightBtn.title = @"删除";  
  124.         [rightBtn setAction:@selector(rightBtnPressed:)];  
  125.         [cloMableView setEditing:NO animated:YES];  
  126.     }  
  127. }  
  128. @end  
效果图:

                     

删除后:



代码比较简单,就不过多解释了!
0 0