美文网首页
UITableView列表新增、删除某一行数据

UITableView列表新增、删除某一行数据

作者: 羊妞麻麻 | 来源:发表于2018-09-18 16:54 被阅读561次

需求如下:


屏幕快照 2018-09-18 下午4.29.18.png

该列表中第一行和第二行的数据是不可以删除的,第三行数据支持删除功能,最后一行数据未新增一行数据的功能。

思路:

看到这个设计,第一想到的就是用UITableView来处理再正常不过了。
其次,常见故障维修、突发故障维修为固定的内容,所以我将这块划定为一种cell样式,新增维修类型同这两个不一样,右边有一个删除按钮的功能,所以我将这块定义为另外一种cell样式.至于最底部+新增维修类型的按钮事件显示,我把它定义为UITableView的tableFooterView。
具体实现如下:
每个VC还是老样子的创建方式

@interface SCChooseMaintenanceTypesVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *addRowBtn;
@property (nonatomic, strong) SCMaintenanceInteractor *interactor;//数据 这个会在项目架构中介绍此用法

@end

@implementation SCChooseMaintenanceTypesVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavItem];
    [self addSubViews];
    [self setConstraints];
    [self initParams];
    [self reloadData];
}

- (void)setNavItem{
    self.isNewNavigationBarStyle = YES;
    self.title = @"新增维修类型";
}

- (void)addSubViews{
    [self.view addSubview:self.tableView];
    self.addRowBtn.hidden = YES;
    self.tableView.tableFooterView = self.addRowBtn;
}

- (void)setConstraints{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).mas_offset(13);
        make.leading.trailing.mas_equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.mas_equalTo(self.view.mas_bottom);
        }
    }];
}

- (void)reloadData{
    @Weakify(self)
    [SQProgressHUD showLoding];
    [self.interactor getAllMaintenanceTypeList:^{
        @Strongify(self)
        self.addRowBtn.hidden = NO;
        [self.tableView reloadData];
        [SQProgressHUD hideLoding];
    } Failure:^{
        [SQProgressHUD hideLoding];
    }];
}

创建TabelViewCell

#pragma mark --UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.interactor.dataArrayM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SCRepairTypeModel *typeModel = [self.interactor.dataArrayM objectAtIndex:indexPath.row];
    
    if (indexPath.row == 0 || indexPath.row == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        cell.backgroundColor = [UIColor whiteColor];
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 18, 140, 24)];
        textLabel.textColor = [UIColor SCTextBlackColor];
        textLabel.font = [UIFont sysFont16];
        textLabel.text = typeModel.name;
        [cell addSubview:textLabel];
        
        UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(kSCREEN_WIDTH-114-15, 18, 120, 24)];
        rightLabel.textColor = [UIColor SCTextLightGrayColor];
        rightLabel.font = [UIFont sysFont14];
        rightLabel.text = @"(默认不可删除)";
        [cell addSubview:rightLabel];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, 58, kSCREEN_WIDTH-30, 2)];
        line.backgroundColor = [UIColor SCTableSeparateColor];
        [cell addSubview:line];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }else{
        __weak typeof(self) weakSelf = self;
        SCEMTypesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCEMTypesCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setCellWithModel:typeModel];
        cell.deleteBtnClick = ^{
            [self.interactor deleteDeviceRepairWithTypeID:typeModel.device_repair_type_id Success:^{
                if (weakSelf.update) {
                    weakSelf.update();
                }
                [weakSelf.tableView reloadData];
            } Failure:nil];
            
        };
       
        return cell;

    }

    return nil;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

懒加载

#pragma mark 懒加载
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
        [_tableView registerClass:[SCEMTypesCell class] forCellReuseIdentifier:@"SCEMTypesCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor SCBackGroundColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _tableView;
}


- (UIButton *)addRowBtn{
    if (!_addRowBtn) {
        _addRowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addRowBtn.frame = CGRectMake(0, 0, kSCREEN_WIDTH, 60);
        _addRowBtn.titleLabel.font = [UIFont fontWithName:@"iconfont" size:16];
        [_addRowBtn setTitleColor:[UIColor SCTextBlueColor] forState:UIControlStateNormal];
        [_addRowBtn setTitle:@"\ue643 新增维修类型" forState:UIControlStateNormal];
        [_addRowBtn setTitleColor:[UIColor SCTextBlueColor] forState:UIControlStateNormal];
        [_addRowBtn setBackgroundColor:[UIColor whiteColor]];
        @Weakify(self)
        [_addRowBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            @Strongify(self)
            SCMaintenanceTypeInputVC *inputVC = [[SCMaintenanceTypeInputVC alloc] init];
            inputVC.scPageConfigKey = @"DEVICE/DETAIL/SERVICE/CATEGORY/INDEX";
            inputVC.appKey = @"FMP";
            inputVC.areaId = self.areaId?:[[SCBUUserDefaultsManager shareUserDefaultsManager] getDataByKey:FacilityRepairRecordSelectAreaIdKey];
            [self goVC:inputVC];
            inputVC.model = ^(SCRepairTypeModel *model) {
                [self.interactor.dataArrayM addObject:model];
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.interactor.dataArrayM.count-1 inSection:0];
                [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                if (self.update) {
                    self.update();
                }
            };
        }];
    }
    return _addRowBtn;
}

- (SCMaintenanceInteractor *)interactor{
    return _interactor = _interactor?:[[SCMaintenanceInteractor alloc] init];
}
@end

其中addRowBtn事件中完成了insertRowsAtIndexPaths在UITabelView中插入一条数据的功能。

inputVC.model = ^(SCRepairTypeModel *model) {
                [self.interactor.dataArrayM addObject:model];
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.interactor.dataArrayM.count-1 inSection:0];
                [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                if (self.update) {
                    self.update();
                }
            };

点击删除按钮完成的事件通过deleteBtnClickBlock完成deleteDeviceRepairWithTypeID来实现

cell.deleteBtnClick = ^{
            [self.interactor deleteDeviceRepairWithTypeID:typeModel.device_repair_type_id Success:^{
                if (weakSelf.update) {
                    weakSelf.update();
                }
                [weakSelf.tableView reloadData];
            } Failure:nil];
            
        };

以上是针对列表中新增、删除某一行数据的具体实现。

相关文章

网友评论

      本文标题:UITableView列表新增、删除某一行数据

      本文链接:https://www.haomeiwen.com/subject/mkvpnftx.html