iOS UITableView侧滑删除

  • 注意:一定是先删除了数据,再执行删除的动画或者其他操作,否则会出现崩溃
#pragma mark ---- 侧滑删除
// 点击了“左滑出现的Delete按钮”会调用这个方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self queyDeletePath:indexPath];

}

//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}

// 修改Delete按钮文字为“删除”
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectType == CarListTypeEidite) {
        return YES;
    }else{
       return NO;
    }
}

//设置进入编辑状态时,Cell不会缩进 
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
      return NO;
}


#pragma mark --- 删除数据的网络请求
- (void)queyDeletePath:(NSIndexPath *)indexPath{
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param SetNoNilObject:[self.dataArr[indexPath.row] cid] forKey:@"carid"];
    [param SetNoNilObject:[User shareUser].mid forKey:@"id"];
    
    wselfCode
    [NetWorkTools POST:deleteCarPath parameters:param success:^(NSDictionary *responseObject, BOOL isSuccess) {
        if ([responseObject GetStringForKey:@"code"].integerValue == 1) {
            
            // 删除模型
            [wself.dataArr removeObjectAtIndex:indexPath.row];
            
            // 刷新
            [wself.mainTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
            
        }else{
            
            [MBProgressHUD showMessage:[responseObject GetStringForKey:@"msg"]];
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}

你可能感兴趣的:(iOS UITableView侧滑删除)