UITableView多选状态 删除
效果图
首先在UITableView的 edittingStyleForRowAtIndexPath函数中,添加如下代码
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
returnUITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
—-初始时设置TableView setEditing=YES;
具体的代码实现和效果图如下:
#import "DuoxuanTableView.h"
@interface DuoxuanTableView ()<UITableViewDelegate,UITableViewDataSource>
{
UIButton * button11;
NSMutableArray * selectedDic;
NSMutableArray * dataArray;
}
@end
@implementation DuoxuanTableView
@synthesize duoxianTable;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
// Do any additional setup after loading the view.
[self initDuoxuanTable];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - tableView代理方法
- (void)initDuoxuanTable {
button11 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
button11.backgroundColor = [UIColor orangeColor];
[button11 addTarget:self action:@selector(rightBtnPressedWithSure:) forControlEvents:UIControlEventTouchUpInside];
[button11 setTitle:@"删除" forState:UIControlStateNormal];
[self.view addSubview:button11];
duoxianTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height-100)];
duoxianTable.delegate = self;
duoxianTable.dataSource = self;
[self.view addSubview:duoxianTable];
// [duoxianTable setEditing:YES animated:YES];
// Do any additional setup after loading the view, typically from a nib.
dataArray = [[NSMutableArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M",@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", nil];
selectedDic = [[NSMutableArray alloc] init];
}
- (void)rightBtnPressedWithSure:(UIButton *)button {
if ([button11.titleLabel.text isEqual:@"删除"]) {
NSLog(@"现在点击的是要选手编辑状态");
[button11 setTitle:@"确定" forState:UIControlStateNormal];
[duoxianTable setEditing:YES animated:YES];
} else if([button11.titleLabel.text isEqual:@"确定"]) {
NSLog(@"现在点击的是删除后 正常状态");
int count = (int)[selectedDic count];
if(count > 0 ) {
for (int i = 0; i < count; i ++) {
NSInteger row = [[ selectedDic objectAtIndex:i] row];
[dataArray removeObjectAtIndex:row];
}
[duoxianTable deleteRowsAtIndexPaths:selectedDic withRowAnimation:UITableViewRowAnimationFade];
[selectedDic removeAllObjects];
[duoxianTable setEditing:NO animated:YES];
[button11 setTitle:@"删除" forState:UIControlStateNormal];
[duoxianTable setEditing:NO animated:YES];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未选中任何数据!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"重新选择", nil];
[alert show];
}
}
}
#pragma -mark
#pragma tableview data source method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [dataArray count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
#pragma tableView delegate methods
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
#pragma tableView delegate methods
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//添加一项
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([button11.titleLabel.text isEqualToString:@"确定"]) {
[selectedDic addObject:indexPath];
NSLog(@"Select---->:%@",selectedDic);
}
}
//取消一项
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([button11.titleLabel.text isEqualToString:@"确定"]) {
[selectedDic removeObject:indexPath];
NSLog(@"Deselect---->:%@",selectedDic);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableViewIdentifier = @"TableViewIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewIdentifier];
NSInteger row = [indexPath row];
cell.textLabel.text = [ dataArray objectAtIndex:row];
return cell;
}
//#pragma mark-
#pragma AlertView delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
[button11 setTitle:@"删除" forState:UIControlStateNormal];
[duoxianTable setEditing:NO animated:YES];
}
}
@end
效果图如下: