ios系列开发之UITableView实现多行删除
2015-09-18 18:16
387 查看
实现多行删除的基本思路:
UITableViewCellEditingStyle返回的如果是UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert; 编辑表的时候就会出现多选按钮.我们可以将所有选中单元格对应的indexPath存放到一个可变数组中,然后点击删除按钮时移除数据中对应的元素.
UITableViewCellEditingStyle返回的如果是UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert; 编辑表的时候就会出现多选按钮.我们可以将所有选中单元格对应的indexPath存放到一个可变数组中,然后点击删除按钮时移除数据中对应的元素.
#import "MainViewController.h" @interface MainViewController () { UIButton * editButton; NSMutableArray * dataArray; NSMutableArray * selectedArray; } @end @implementation MainViewController - (void)dealloc{ [dataArray release]; dataArray = nil; [_tableView release]; _tableView = nil; [super dealloc]; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480- 44) style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [self setTableViewHeaderView]; [self.view addSubview:_tableView]; dataArray = [[NSMutableArray alloc]initWithObjects:@"大葫芦",@"大娃",@"二娃",@"三娃",@"四娃",@"五娃",@"六娃",@"七娃", nil]; selectedArray = [[NSMutableArray alloc]init]; //非ARC模式不能使用静态方法创建全局变量 } - (void)setTableViewHeaderView{ UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)]; editButton = [UIButton buttonWithType:UIButtonTypeCustom]; editButton.frame = CGRectMake(120, 5, 80, 50); [editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [editButton setTitle:@"delete" forState:UIControlStateNormal]; [editButton addTarget:self action:@selector(deleteSeletions:) forControlEvents:UIControlEventTouchUpInside]; [view addSubview:editButton]; self.tableView.tableHeaderView = view; } #pragma mark - 删除按钮的监听事件 - (void)deleteSeletions:(UIButton *)button{ NSString * title = self.tableView.editing ? @"delete" : @"ok"; [button setTitle:title forState:UIControlStateNormal]; self.tableView.editing = !self.tableView.editing; int count = selectedArray.count; if (count == 0) { return; }else { //冒泡排序 将indexPath.row 进行从小到大排序,避免删除数据时出现数组越界的BUG for (int i = 0 ; i < count - 1; i ++) { for (int j = 0 ; j < count - 1 - i; j++) { NSIndexPath * indexPath1 = [selectedArray objectAtIndex:j]; NSIndexPath * indexPath2 = [selectedArray objectAtIndex:j+1]; if (indexPath1.row > indexPath2.row) { [selectedArray exchangeObjectAtIndex:j withObjectAtIndex:j+1]; } } } for (int i = count-1 ; i >= 0; i--) { //从大到小删除对应的数据 NSIndexPath * indexPath = [selectedArray objectAtIndex:i]; [dataArray removeObjectAtIndex:indexPath.row]; } [selectedArray removeAllObjects]; [self.tableView reloadData]; } } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString * cellID = @"cellID"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease]; } cell.textLabel.text = [dataArray objectAtIndex:indexPath.row]; return cell; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } #pragma mark - 选中单元格时方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if ([editButton.titleLabel.text isEqualToString:@"ok"]) { [selectedArray addObject:indexPath]; } } #pragma mark - 取消选中单元格方法 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ if ([editButton.titleLabel.text isEqualToString:@"ok"]) { [selectedArray removeObject:indexPath]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- 如何设置UITextField的焦点?
- How-to: use spark to suport query across mysql tables and hbase tables
- iOS探究UITableView的内部代码,仿UITableView自定义
- QEMU Guest Agent
- ios开发系列之UITableView的移动行,编辑行,打开关闭区,刷新表
- UIView的autoresizingMask属性
- UI:沙盒
- Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
- JAVA中的优先级队列PriorityQueue
- iOS中的UITextView
- Samsung手机ClipboardUIManager引起的内存泄漏
- iOS中的UISearchBar
- iOS中的UIDatePicker 日期选择器
- iOS中的UIAlertContrller
- LigerUI之Grid使用详解(一)——显示数据
- ios9学习系列:UIStackView
- UITextView汉字光标问题
- iOS7 UITextViewDidChange - crashes
- Easyui
- UITableView动态改变Cell高度