UITableView实现左滑删除的几种方式

一 、UITableView左滑删除实现方式之一:

//进入编辑模式,按下出现的编辑按钮后,进行删除操作  iOS8必须实现这个代理方法才会有侧滑效果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    DeclareWeakSelf
    UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        // 删除操作
        [weakSelf.phonesTable beginUpdates];
        LJPerson *person = nil;
        LJSectionPerson *section = weakSelf.sortedPersons[indexPath.section];
        person = section.persons[indexPath.row];
        [[LJContactManager sharedInstance] deleteContactWithPerson:person];
        
        if (section.persons.count > 2 ) {//删除row
            NSMutableArray *sectionPersonsArray = [section.persons mutableCopy];
            [sectionPersonsArray removeObjectAtIndex:indexPath.row];
            section.persons = sectionPersonsArray;    //清除数据源!
            [weakSelf.phonesTable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }else {//删除section
            [weakSelf.sortedPersons removeObjectAtIndex:indexPath.section];
            [weakSelf.keys removeObjectAtIndex:indexPath.section];
            [weakSelf.phonesTable deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        
        [weakSelf.phonesTable endUpdates];
        [weakSelf.phonesTable reloadData];
    }];
    return @[deleteRoWAction];
}

 

二 、UITableView左滑删除实现方式之二:

 (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    //第二组可以左滑删除
    if (indexPath.section == 2) {
        return YES;
    }
    
    return NO;
}
 
// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
 
// 进入编辑模式,按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        if (indexPath.section == 2) {
            
        } 
    }
}
 
// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

 

三 、UITableView左滑删除实现方式之三:

//iOS11 后的新方法,,  可以设置image和title
- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    //删除
    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        [self.titleArr removeObjectAtIndex:indexPath.row];
        completionHandler (YES);
        [self.tableView reloadData];
    }];
    deleteRowAction.image = [UIImage imageNamed:@"删除"];
    deleteRowAction.backgroundColor = [UIColor redColor];
    
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
    return config;
}

 

注:::判断UITableView的滚动方向 And 判断UITableView是否滚动到了最底部

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentSize.height > ScreenHeight) {
        CGFloat height = scrollView.frame.size.height;
        CGFloat contentOffsetY = scrollView.contentOffset.y;
        CGFloat bottomOffset = scrollView.contentSize.height - contentOffsetY;
        if (bottomOffset <= height){
            //tableView滑动到最底部
        }else {
           //tableView没有滑动到最底部
        }
        
        //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势
        CGPoint slide = [scrollView.panGestureRecognizer velocityInView:scrollView];
        //获取到拖拽的速度 >0 向下拖动; <0 向上拖动  =0停止拖拽
        if (slide.y > 0 ) { 
            //tableView向下拖动
        }
    }
}

 

 

 

 

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值