// // PBSeleceArtifactsViewController.m // IphoneBIMe // // Created by zjf on 2021/1/14. // Copyright © 2021 ProBIM. All rights reserved. // #import "PBSeleceArtifactsViewController.h" #import "PBSelectArtifactsCell.h" #import "PBSelectTaskCell.h" #import "PBArtifactsInfoModel.h" #import "PBETaskInfoModel.h" #import "PBAddExamineViewController.h" static NSString *const cellID = @"cellID"; static NSString *const TaskCellID = @"TaskCellID"; @interface PBSeleceArtifactsViewController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, weak) UIButton *determineBtn; @property (nonatomic, copy) NSString *titleText; @end @implementation PBSeleceArtifactsViewController - (void)viewDidLoad { [super viewDidLoad]; [self setupNav]; [self setupUI]; } - (void)setupNav { self.title = [NSString stringWithFormat:@"%@ (%zd)",self.titleText, self.selectList.count]; UIBarButtonItem *backNavItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Doc_preview_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backItemAction)]; self.navigationItem.leftBarButtonItem = backNavItem; } - (void)backItemAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)setupUI { self.view.backgroundColor = PBColor(244, 245, 246); self.tableView = [[UITableView alloc] init]; self.tableView.backgroundColor = PBColor(244, 245, 246); self.tableView.dataSource = self; self.tableView.delegate = self; self.tableView.bounces = NO; self.tableView.rowHeight = 56; self.tableView.sectionIndexColor = PBColor(97, 111, 125); UITableView *footerV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; if (@available(iOS 15.0, *)) { footerV.sectionHeaderTopPadding = 0; }; self.tableView.tableFooterView = footerV; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.tableView registerClass:[PBSelectArtifactsCell class] forCellReuseIdentifier:cellID]; [self.tableView registerClass:[PBSelectTaskCell class] forCellReuseIdentifier:TaskCellID]; self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 56, 0); [self.view addSubview:self.tableView]; CGFloat bottomH = 0.f; if (IS_IPHONE_X) { bottomH = 0.f + IPHONE_X_BOTTOM; } [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view); make.left.right.equalTo(self.view); make.bottom.equalTo(self.view).offset(-(bottomH)); }]; UIView *bottomV = [[UIView alloc] init]; bottomV.backgroundColor = [UIColor whiteColor]; [self.view addSubview:bottomV]; [bottomV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.bottom.right.equalTo(self.tableView); make.height.equalTo(@54); }]; UIButton *determineBtn = [UIButton z_textButton:[NSString stringWithFormat:@"确定(%zd)",self.selectList.count] boldFontSize:14 normalColor:[UIColor whiteColor]]; [determineBtn addTarget:self action:@selector(determineBtnAction) forControlEvents:UIControlEventTouchUpInside]; determineBtn.backgroundColor = PBColor(0, 122, 255); [self.view addSubview:determineBtn]; [determineBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bottomV).offset(9); make.right.equalTo(bottomV).offset(-20); make.size.mas_equalTo(CGSizeMake(89, 34)); }]; [determineBtn circleViewWithRadius:6]; self.determineBtn = determineBtn; } - (void)determineBtnAction { if ([self.type isEqualToString:@"Artifacts"]) { [PBNoteCenter postNotificationName:PBNoteCenterUpdateArtifacts object:self.selectList.copy]; }else if ([self.type isEqualToString:@"Task"]) { [PBNoteCenter postNotificationName:PBNoteCenterUpdateTask object:self.selectList.copy]; } [self backAction]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.selectList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.type isEqualToString:@"Artifacts"]) { PBSelectArtifactsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; PBArtifactsInfoModel *infoModel = self.selectList[indexPath.row]; cell.artifactsInfoModel = infoModel; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; }else if ([self.type isEqualToString:@"Task"]) { PBSelectTaskCell *cell = [tableView dequeueReusableCellWithIdentifier:TaskCellID forIndexPath:indexPath]; PBETaskInfoModel *infoModel = self.selectList[indexPath.row]; cell.taskInfoModel = infoModel; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; }else { UITableViewCell *cell = [[UITableViewCell alloc] init]; return cell; } } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { //先删数据 再删UI [self.selectList removeObjectAtIndex:indexPath.row]; self.title = [NSString stringWithFormat:@"%@ (%zd)",self.titleText, self.selectList.count]; [self.determineBtn setTitle:[NSString stringWithFormat:@"确定(%zd)",self.selectList.count] forState:UIControlStateNormal]; // if (self.selectList.count == 0) { // self.determineBtn.enabled = NO; // } [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView reloadData]; }]; return @[deleteAction]; } -(void)backAction{ UINavigationController *navVC = self.navigationController; NSMutableArray *viewControllers = [[NSMutableArray alloc] init]; for (UIViewController *vc in [navVC viewControllers]) { [viewControllers addObject:vc]; if ([vc isKindOfClass:[PBAddExamineViewController class]]) { break; } } [navVC setViewControllers:viewControllers animated:YES]; } - (void)setSelectList:(NSMutableArray *)selectList { _selectList = selectList; if (selectList == nil) { _selectList = [[NSMutableArray alloc] init]; } } - (void)setType:(NSString *)type { _type = type; if ([type isEqualToString:@"Artifacts"]) { self.titleText = @"构件"; }else if([type isEqualToString:@"Task"]) { self.titleText = @"任务"; } } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end