UItableView批量删除

来源:互联网 发布:mysql表分区上亿的数据 编辑:程序博客网 时间:2024/05/22 03:16

具体实现:

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

  117. #pragma AlertView delegate method  
  118. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{  
  119.     if (buttonIndex == 0) {  
  120.         rightBtn.title = @"删除";  
  121.         [rightBtn setAction:@selector(rightBtnPressed:)];  
  122.         [cloMableView setEditing:NO animated:YES];  
  123.     }  
  124. }  
  125. @end 
0 0
原创粉丝点击