歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iPhone開發之實現UITableView多選刪除功能詳解

iPhone開發之實現UITableView多選刪除功能詳解

日期:2017/3/1 10:18:41   编辑:Linux編程

很多情況下應用需要批量處理功能,但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. #import "CloViewController.h"
  9. @interface CloViewController ()
  10. @end
  11. @implementation CloViewController
  12. @synthesize dataArray;
  13. @synthesize selectedDic;
  14. - (IBAction)rightBtnPressed:(id)sender{
  15. //顯示多選圓圈
  16. [cloMableView setEditing:YES animated:YES];
  17. rightBtn.title = @"確定";
  18. [rightBtn setAction:@selector(rightBtnPressedWithSure:)];
  19. }
  20. - (IBAction)rightBtnPressedWithSure:(id)sender{
  21. //do something with selected cells like delete
  22. // NSLog(@"selectedDic------->:%@", self.selectedDic);
  23. int count = [self.selectedDic count];
  24. if (count > 0 ) {
  25. for (int i = 0; i < count; i++) {
  26. NSInteger row = [[self.selectedDic objectAtIndex:i] row];
  27. [self.dataArray removeObjectAtIndex:row];
  28. }
  29. // NSLog(@"self.dataArray:------>:%@", self.dataArray);
  30. [cloMableView deleteRowsAtIndexPaths:self.selectedDic withRowAnimation:UITableViewRowAnimationFade];
  31. [self.selectedDic removeAllObjects];
  32. // NSLog(@"self.selectedDic--------->:%@", self.selectedDic);
  33. // [cloMableView reloadData];
  34. rightBtn.title = @"刪除";
  35. [rightBtn setAction:@selector(rightBtnPressed:)];
  36. [cloMableView setEditing:NO animated:YES];
  37. }else {
  38. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未選中任何數據!" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:@"重新選擇", nil];
  39. [alert show];
  40. [alert release];
  41. }
  42. }
  43. - (void)viewDidLoad
  44. {
  45. [super viewDidLoad];
  46. // Do any additional setup after loading the view, typically from a nib.
  47. self.dataArray = [[NSMutableArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", nil];
  48. self.selectedDic = [[NSMutableArray alloc] init];
  49. rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"刪除" style:UIBarButtonItemStyleBordered target:self action:@selector(rightBtnPressed:)];
  50. self.navigationItem.rightBarButtonItem = rightBtn;
  51. }
  52. - (void)viewDidUnload
  53. {
  54. [super viewDidUnload];
  55. // Release any retained subviews of the main view.
  56. }
  57. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  58. {
  59. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  60. }
  61. #pragma -mark
  62. #pragma tableview data source method
  63. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  64. return [self.dataArray count];
  65. }
  66. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  67. return 1;
  68. }
  69. #pragma tableView delegate methods
  70. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
  71. return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
  72. }
  73. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
  74. return YES;
  75. }
  76. //添加一項
  77. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  78. if ([rightBtn.title isEqualToString:@"確定"]) {
  79. [self.selectedDic addObject:indexPath];
  80. // NSLog(@"Select---->:%@",self.selectedDic);
  81. }
  82. }
  83. //取消一項
  84. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
  85. if ([rightBtn.title isEqualToString:@"確定"]) {
  86. [self.selectedDic removeObject:indexPath];
  87. // NSLog(@"Deselect---->:%@",self.selectedDic);
  88. }
  89. }
  90. //選擇後
  91. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
  92. //
  93. }
  94. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  95. static NSString *tableViewIdentifier = @"TableViewIdentifier";
  96. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewIdentifier];
  97. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewIdentifier];
  98. NSInteger row = [indexPath row];
  99. cell.textLabel.text = [self.dataArray objectAtIndex:row];
  100. return cell;
  101. }
  102. #pragma mark-
  103. #pragma AlertView delegate method
  104. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  105. if (buttonIndex == 0) {
  106. rightBtn.title = @"刪除";
  107. [rightBtn setAction:@selector(rightBtnPressed:)];
  108. [cloMableView setEditing:NO animated:YES];
  109. }
  110. }
  111. @end
效果圖:

刪除後:



代碼比較簡單,就不過多解釋了!
Copyright © Linux教程網 All Rights Reserved