当前位置: 首页>编程日记>正文

iOS11-UITableView侧滑删除自定义图片

iOS11-UITableView侧滑删除自定义图片
#pragma mark ========== tableview侧滑删除 S===================
//开启tableview的编辑模式
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        //开启侧滑删除
        return YES;
    }else{
        return NO;
    }

}
//如果想在侧拉的时候显示是中文的删除,只需要用下面的方法替换掉上面的方法就好了
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){//title可自已定义
        //删除收货地址
        YZMyAddressListModel *modelCell =self.dataSouce[indexPath.row];
        [self deleteAddressWithModel:modelCell];
        //刷新表格
        [self.dataSouce removeObject:modelCell];//删除数据源当前行数据
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView reloadData];
        //空白页处理
        if (self.dataSouce.count>0) {
              self.noDataView.hidden = YES;
        }else{
            self.noDataView.hidden = NO;
        }
    }];
    return @[deleteRoWAction];
}

//iOS11之后侧滑删除-支持设置图片【设置后iOS11优先走这里,与上面不冲突】
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath  API_AVAILABLE(ios(11.0)){
    //删除
    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        //删除收货地址
        YZMyAddressListModel *modelCell =self.dataSouce[indexPath.row];
        [self deleteAddressWithModel:modelCell];
        //刷新表格
        [self.dataSouce removeObject:modelCell];//删除数据源当前行数据
        //        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView reloadData];
        //空白页处理
        if (self.dataSouce.count>0) {
            self.noDataView.hidden = YES;
        }else{
            self.noDataView.hidden = NO;
        }
        completionHandler (YES);
    }];
    deleteRowAction.image = [UIImage imageNamed:@"侧滑删除-删除"];
    deleteRowAction.backgroundColor = [UIColor redColor];

    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
    return config;
}
#pragma mark ========== tableview侧滑删除 E===================

相关文章: