UITableView 编辑模式(增加-删除-移动---自定义左滑 title)

Posted Da雪山

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UITableView 编辑模式(增加-删除-移动---自定义左滑 title)相关的知识,希望对你有一定的参考价值。

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.dataArray = [NSMutableArray arrayWithArray: @[@"1",@"2",@"3",@"4",@"5",@"6",@"7"]]; [self createView]; self.view.backgroundColor = [UIColor greenColor]; } -(void)createView{ self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ce"]; // self.tableView.bounces = 0; // self.tableView.showsVerticalScrollIndicator = 0; self.tableView.tableFooterView = [[UIView alloc]init];//没有的不展示 line UIBarButtonItem *deleteBar = [[UIBarButtonItem alloc]initWithTitle:@"删除" style: UIBarButtonItemStylePlain target:self action:@selector(deleteBtn)]; self.navigationItem.rightBarButtonItem = deleteBar; UIBarButtonItem *addBar1 = [[UIBarButtonItem alloc]initWithTitle:@"添加1 " style: UIBarButtonItemStylePlain target:self action:@selector(addBtn)]; UIBarButtonItem *addBar2 = [[UIBarButtonItem alloc]initWithTitle:@" 添加2(模式)" style: UIBarButtonItemStylePlain target:self action:@selector(addBtn2)]; self.navigationItem.leftBarButtonItems = @[addBar1,addBar2]; } //删除(进入编辑模式) - (void)deleteBtn{ self.isAdd = NO; static int flog = 1; flog ^= 1; if (flog) { [self.tableView setEditing:YES animated:YES]; }else{ [self.tableView setEditing:NO animated:YES]; } } //增加1 - (void)addBtn{ [self.dataArray addObject:@"新增一条"]; //在 <指定行> 添加 NSIndexPath * indexPath = [NSIndexPath indexPathForRow:self.dataArray.count - 1 inSection:0]; [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight]; } - (void)addBtn2{ self.isAdd = YES; static int flog = 1; flog ^= 1; if (flog) { [self.tableView setEditing:YES animated:YES]; }else{ [self.tableView setEditing:NO animated:YES]; } } #pragma mark --dataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ce"]; cell.textLabel.text = self.dataArray[indexPath.row]; return cell; } #pragma mark -- delegate 编辑模式(增加/删除/移动) - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ if (self.isAdd) { return UITableViewCellEditingStyleInsert; //添加 }else{ return UITableViewCellEditingStyleDelete; //删除 } } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ NSInteger sourceRow = indexPath.row; //删除 if (editingStyle == UITableViewCellEditingStyleDelete) { [self.dataArray removeObjectAtIndex:sourceRow]; [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft]; } //添加(点击+号按钮) else if (editingStyle == UITableViewCellEditingStyleInsert){ [self.dataArray insertObject:@"新增" atIndex: sourceRow+1]; //在指定位置插入 NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:sourceRow+1 inSection:indexPath.section]; [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil] withRowAnimation:UITableViewRowAnimationRight]; } } //移动(只有写了这个方法才能移动,但此时不能左滑) - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ NSInteger sourceRow = sourceIndexPath.row; NSInteger destinRow = destinationIndexPath.row; NSString *moveObject = [self.dataArray objectAtIndex:sourceRow]; [self.dataArray removeObjectAtIndex:sourceRow]; [self.dataArray insertObject:moveObject atIndex:destinRow]; //改变在<对象>数组中的位置 } #pragma mark --左滑选项(title可自已定义) -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ //1.删除 UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" 删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定义 NSLog(@"点击了-删除"); [self.dataArray removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft]; }]; deleteRoWAction.backgroundColor = [UIColor greenColor]; //定义button的颜色,默认是红色的 //test UITableViewRowAction *test = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" 置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定义 NSLog(@"点击了-test"); }]; test.backgroundColor = [UIColor blueColor]; return @[deleteRoWAction,test];//最后返回这俩个RowAction 的数组 }

以上是关于UITableView 编辑模式(增加-删除-移动---自定义左滑 title)的主要内容,如果未能解决你的问题,请参考以下文章

如何启用移动但禁用 UITableView 中某些行的删除

iOS - UITableView 编辑(cell的插入, 删除, 移动)

iOS - 允许在不编辑模式的情况下在“UITableView”中移动默认行

编辑模式 UITableView,不能阻止内容视图向右移动

UITableView数据的添加删除移动

UITableview 编辑模式时不出现左删除图标