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向下拖動
        }
    }
}

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章