您的位置:首页 > 产品设计 > UI/UE

ios系列开发之UITableView实现多行删除

2015-09-18 18:16 387 查看
实现多行删除的基本思路:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: