zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
//  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 ()<UITableViewDelegate, UITableViewDataSource>
@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<UITableViewRowAction *> *)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