Skip to content

Commit f4f9fb9

Browse files
committed
tag 选择的逻辑
1 parent eb0d177 commit f4f9fb9

7 files changed

Lines changed: 146 additions & 198 deletions

File tree

Coding_iOS/Controllers/EditLabelViewController.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
#import "BaseViewController.h"
1010

11-
@class ProjectTopic;
12-
@interface EditLabelViewController : BaseViewController
11+
@class Project;
1312

14-
@property (weak, nonatomic) ProjectTopic *curProTopic;
15-
@property (assign, nonatomic) BOOL isSaveChange;
13+
@interface EditLabelViewController : BaseViewController
14+
@property (strong, nonatomic) NSArray *orignalTags;
15+
@property (strong, nonatomic) Project *curProject;
1616

17-
@property (copy, nonatomic) void(^topicChangedBlock)();
17+
@property (copy, nonatomic) void(^tagsChangedBlock)(EditLabelViewController *vc, NSMutableArray *selectedTags);
1818

1919
@end

Coding_iOS/Controllers/EditLabelViewController.m

Lines changed: 65 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@
1919
#define kCellIdentifier_EditLabelCell @"EditLabelCell"
2020

2121
@interface EditLabelViewController () <UITableViewDataSource, UITableViewDelegate, SWTableViewCellDelegate, UITextFieldDelegate>
22-
{
23-
NSString *_tempLabel;
24-
NSMutableArray *_tempArray;
25-
}
26-
@property (strong, nonatomic) NSMutableArray *labels;
27-
22+
@property (strong, nonatomic) NSString *tagNameToAdd;
23+
@property (strong, nonatomic) NSMutableArray *tagList, *selectedTags;
2824
@property (strong, nonatomic) TPKeyboardAvoidingTableView *myTableView;
29-
30-
@property (nonatomic, weak) UITextField *mCurrentTextField;
3125
@end
3226

3327
@implementation EditLabelViewController
@@ -41,20 +35,24 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
4135
return self;
4236
}
4337

38+
- (void)setOrignalTags:(NSArray *)orignalTags{
39+
_orignalTags = orignalTags;
40+
if (_orignalTags.count > 0) {
41+
_selectedTags = [_orignalTags mutableCopy];
42+
}else{
43+
_selectedTags = [NSMutableArray new];
44+
}
45+
}
46+
4447
- (void)viewDidLoad {
4548
[super viewDidLoad];
4649
// Do any additional setup after loading the view.
4750
self.title = @"标签管理";
4851

49-
//if (!_isSaveChange) {
50-
self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithBtnTitle:@"完成" target:self action:@selector(okBtnClick)];
51-
self.navigationItem.rightBarButtonItem.enabled = FALSE;
52-
//}
52+
self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithBtnTitle:@"完成" target:self action:@selector(okBtnClick)];
53+
self.navigationItem.rightBarButtonItem.enabled = FALSE;
5354

5455
self.view.backgroundColor = kColorTableSectionBg;
55-
56-
_labels = [[NSMutableArray alloc] initWithCapacity:4];
57-
5856
_myTableView = ({
5957
TPKeyboardAvoidingTableView *tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
6058
tableView.backgroundColor = kColorTableSectionBg;
@@ -74,111 +72,45 @@ - (void)viewDidLoad {
7472
- (void)viewWillAppear:(BOOL)animated
7573
{
7674
[super viewWillAppear:animated];
77-
[_labels removeAllObjects];
7875
[self sendRequest];
7976
}
8077

81-
- (void)didReceiveMemoryWarning {
82-
[super didReceiveMemoryWarning];
83-
// Dispose of any resources that can be recreated.
84-
}
85-
86-
- (void)dealloc
87-
{
88-
_myTableView.delegate = nil;
89-
_myTableView.dataSource = nil;
90-
}
91-
92-
- (void)setCurProTopic:(ProjectTopic *)curProTopic
93-
{
94-
_curProTopic = curProTopic;
95-
_tempArray = [NSMutableArray arrayWithArray:_curProTopic.mdLabels];
96-
}
97-
9878
- (void)sendRequest
9979
{
10080
[self.view beginLoading];
101-
10281
__weak typeof(self) weakSelf = self;
103-
104-
[[Coding_NetAPIManager sharedManager] request_TagListInProject:_curProTopic.project type:ProjectTagTypeTopic andBlock:^(id data, NSError *error) {
82+
[[Coding_NetAPIManager sharedManager] request_TagListInProject:_curProject type:ProjectTagTypeTopic andBlock:^(id data, NSError *error) {
10583
[weakSelf.view endLoading];
10684
if (data) {
107-
[_labels addObjectsFromArray:data];
108-
for (ProjectTag *lbl in _labels) {
109-
for (ProjectTag *tLbl in _curProTopic.mdLabels) {
110-
if ([lbl.id integerValue] == [tLbl.id integerValue]) {
111-
tLbl.name = lbl.name;
112-
break;
113-
}
114-
}
115-
for (ProjectTag *tLbl in _tempArray) {
116-
if ([lbl.id integerValue] == [tLbl.id integerValue]) {
117-
tLbl.name = lbl.name;
118-
break;
119-
}
120-
}
121-
}
85+
weakSelf.tagList = data;
12286
[weakSelf.myTableView reloadData];
12387
}
124-
12588
}];
12689
}
12790

128-
- (NSString *)toDelPath:(NSInteger)index
129-
{
130-
ProjectTag *ptLabel = [_labels objectAtIndex:index];
131-
return [NSString stringWithFormat:@"api/project/%d/topic/label/%lld", _curProTopic.project_id.intValue, ptLabel.id.longLongValue];
132-
}
133-
134-
- (NSString *)toMedifyPath:(NSNumber *)labelID
135-
{
136-
return [NSString stringWithFormat:@"api/topic/%d/label/%lld", _curProTopic.id.intValue, labelID.longLongValue];
137-
}
138-
13991
#pragma mark - click
14092
- (void)okBtnClick
14193
{
142-
_curProTopic.mdLabels = _tempArray;
143-
144-
self.navigationItem.rightBarButtonItem.enabled = NO;
145-
if (_isSaveChange) {
146-
147-
@weakify(self);
148-
[[Coding_NetAPIManager sharedManager] request_ModifyProjectTpoicLabel:self.curProTopic andBlock:^(id data, NSError *error) {
149-
@strongify(self);
150-
self.navigationItem.rightBarButtonItem.enabled = YES;
151-
if (data) {
152-
_curProTopic.labels = [NSMutableArray arrayWithArray:_curProTopic.mdLabels];
153-
if (self.topicChangedBlock) {
154-
self.topicChangedBlock();
155-
}
156-
[self.navigationController popViewControllerAnimated:YES];
157-
}
158-
}];
159-
} else {
160-
if (self.topicChangedBlock) {
161-
self.topicChangedBlock();
162-
}
163-
[self.navigationController popViewControllerAnimated:YES];
94+
if (self.tagsChangedBlock) {
95+
self.tagsChangedBlock(self, _selectedTags);
16496
}
16597
}
16698

16799
- (void)addBtnClick:(UIButton *)sender
168100
{
169-
[_mCurrentTextField resignFirstResponder];
170-
if (_tempLabel.length > 0) {
101+
[self.view endEditing:YES];
102+
if (_tagNameToAdd.length > 0) {
171103
__weak typeof(self) weakSelf = self;
172-
ProjectTag *curTag = [ProjectTag tagWithName:_tempLabel];
173-
[[Coding_NetAPIManager sharedManager] request_AddTag:curTag toProject:_curProTopic.project andBlock:^(id data, NSError *error) {
104+
ProjectTag *curTag = [ProjectTag tagWithName:_tagNameToAdd];
105+
[[Coding_NetAPIManager sharedManager] request_AddTag:curTag toProject:_curProject andBlock:^(id data, NSError *error) {
174106
if (data) {
175107
curTag.id = data;
176-
[weakSelf.labels addObject:curTag];
108+
[weakSelf.tagList addObject:curTag];
109+
weakSelf.tagNameToAdd = @"";
177110
[weakSelf.myTableView reloadData];
178-
_tempLabel = @"";
179-
weakSelf.mCurrentTextField.text = @"";
180-
[weakSelf showHudTipStr:@"添加标签成功^^"];
181111
sender.enabled = FALSE;
112+
113+
[weakSelf showHudTipStr:@"添加标签成功^^"];
182114
}
183115
}];
184116
}
@@ -199,14 +131,14 @@ - (void)showHudTipStr:(NSString *)tipStr
199131
#pragma mark - UITableViewDataSource
200132
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
201133
{
202-
return _labels.count > 0 ? 2 : 1;
134+
return _tagList.count > 0 ? 2 : 1;
203135
}
204136

205137
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
206138
{
207139
NSInteger row = 1;
208140
if (section == 1) {
209-
row = _labels.count;
141+
row = _tagList.count;
210142
}
211143
return row;
212144
}
@@ -215,31 +147,30 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
215147
{
216148
if (indexPath.section == 0) {
217149
EditLabelHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_EditLabelHeadCell forIndexPath:indexPath];
218-
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
219150
[cell.addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
220151
cell.labelField.delegate = self;
221152
[cell.labelField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
222-
cell.backgroundColor = kColorTableBG;
153+
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
223154
return cell;
224-
}
225-
226-
ProjectTag *ptLabel = _labels[indexPath.row];
227-
228-
EditLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_EditLabelCell forIndexPath:indexPath];
229-
[cell setRightUtilityButtons:[self rightButtons] WithButtonWidth:[EditLabelCell cellHeight]];
230-
cell.delegate = self;
231-
232-
BOOL selected = FALSE;
233-
for (ProjectTag *lbl in _curProTopic.mdLabels) {
234-
if ([lbl.id integerValue] == [ptLabel.id integerValue]) {
235-
selected = TRUE;
236-
break;
155+
}else{
156+
ProjectTag *ptLabel = _tagList[indexPath.row];
157+
158+
EditLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_EditLabelCell forIndexPath:indexPath];
159+
[cell setRightUtilityButtons:[self rightButtons] WithButtonWidth:[EditLabelCell cellHeight]];
160+
cell.delegate = self;
161+
162+
BOOL selected = FALSE;
163+
for (ProjectTag *lbl in _selectedTags) {
164+
if ([lbl.id integerValue] == [ptLabel.id integerValue]) {
165+
selected = TRUE;
166+
break;
167+
}
237168
}
169+
[cell setTag:ptLabel andSelected:selected];
170+
171+
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
172+
return cell;
238173
}
239-
[cell setTag:ptLabel andSelected:selected];
240-
241-
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
242-
return cell;
243174
}
244175

245176
#pragma mark - UITableViewDelegate
@@ -258,37 +189,20 @@ - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSInde
258189

259190
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
260191
{
192+
[self.view endEditing:YES];
261193
[tableView deselectRowAtIndexPath:indexPath animated:YES];
262-
[_mCurrentTextField resignFirstResponder];
263194

264195
if (indexPath.section > 0) {
265196
EditLabelCell *cell = (EditLabelCell *)[tableView cellForRowAtIndexPath:indexPath];
266197
cell.selectBtn.selected = !cell.selectBtn.selected;
267198

268-
ProjectTag *lbl = _labels[indexPath.row];
269-
270-
if (cell.selectBtn.selected) {
271-
BOOL add = TRUE;
272-
for (ProjectTag *tempLbl in _tempArray) {
273-
if ([tempLbl.id integerValue] == [lbl.id integerValue]) {
274-
add = FALSE;
275-
break;
276-
}
277-
}
278-
if (add) {
279-
[_tempArray addObject:lbl];
280-
self.navigationItem.rightBarButtonItem.enabled = YES;
281-
}
282-
} else {
283-
for (ProjectTag *tempLbl in _tempArray) {
284-
if ([tempLbl.id integerValue] == [lbl.id integerValue]) {
285-
286-
[_tempArray removeObject:tempLbl];
287-
self.navigationItem.rightBarButtonItem.enabled = YES;
288-
break;
289-
}
290-
}
199+
ProjectTag *tagInSelected = [ProjectTag tags:_selectedTags hasTag:_tagList[indexPath.row]];
200+
if (cell.selectBtn.selected && !tagInSelected) {
201+
[_selectedTags addObject:_tagList[indexPath.row]];
202+
}else if (!cell.selectBtn.selected && tagInSelected){
203+
[_selectedTags removeObject:tagInSelected];
291204
}
205+
self.navigationItem.rightBarButtonItem.enabled = ![ProjectTag tags:_selectedTags isEqualTo:_orignalTags];
292206
}
293207
}
294208

@@ -321,7 +235,7 @@ - (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityBut
321235
[self renameBtnClick:indexPath.row];
322236
} else {
323237
__weak typeof(self) weakSelf = self;
324-
ProjectTag *ptLabel = [_labels objectAtIndex:indexPath.row];
238+
ProjectTag *ptLabel = [_tagList objectAtIndex:indexPath.row];
325239
NSString *tip = [NSString stringWithFormat:@"确定要删除标签:%@", ptLabel.name];
326240
UIActionSheet *actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle:tip buttonTitles:nil destructiveTitle:@"确认删除" cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
327241
if (index == 0) {
@@ -335,15 +249,15 @@ - (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityBut
335249
- (void)renameBtnClick:(NSInteger)index
336250
{
337251
ResetLabelViewController *vc = [[ResetLabelViewController alloc] init];
338-
vc.ptLabel = [_labels objectAtIndex:index];
339-
vc.curProject = _curProTopic.project;
252+
vc.ptLabel = [_tagList objectAtIndex:index];
253+
vc.curProject = _curProject;
340254
[self.navigationController pushViewController:vc animated:YES];
341255
}
342256

343257
- (void)deleteBtnClick:(NSInteger)index
344258
{
345259
__weak typeof(self) weakSelf = self;
346-
[[Coding_NetAPIManager sharedManager] request_DeleteTag:_labels[index] inProject:_curProTopic.project andBlock:^(id data, NSError *error) {
260+
[[Coding_NetAPIManager sharedManager] request_DeleteTag:_tagList[index] inProject:_curProject andBlock:^(id data, NSError *error) {
347261
if (data) {
348262
[weakSelf deleteLabel:index];
349263
}
@@ -352,41 +266,27 @@ - (void)deleteBtnClick:(NSInteger)index
352266

353267
- (void)deleteLabel:(NSInteger)index
354268
{
355-
ProjectTag *lbl = _labels[index];
356-
for (ProjectTag *tempLbl in _tempArray) {
269+
ProjectTag *lbl = _tagList[index];
270+
for (ProjectTag *tempLbl in _selectedTags) {
357271
if ([tempLbl.id integerValue] == [lbl.id integerValue]) {
358-
[_tempArray removeObject:tempLbl];
272+
[_selectedTags removeObject:tempLbl];
359273
self.navigationItem.rightBarButtonItem.enabled = YES;
360274
break;
361275
}
362276
}
363-
[self.labels removeObjectAtIndex:index];
277+
[self.tagList removeObjectAtIndex:index];
364278
[self.myTableView reloadData];
365279
}
366280

367281
#pragma mark - UITextFieldDelegate
368-
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
369-
{
370-
self.mCurrentTextField = textField;
371-
return YES;
372-
}
373-
374-
- (void)textFieldDidEndEditing:(UITextField *)textField
375-
{
376-
}
377-
378-
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
379-
{
380-
return YES;
381-
}
382282

383283
- (void)textFieldDidChange:(UITextField *)textField
384284
{
385-
_tempLabel = [textField.text trimWhitespace];
386-
BOOL enabled = _tempLabel.length > 0 ? TRUE : FALSE;
285+
_tagNameToAdd = [textField.text trimWhitespace];
286+
BOOL enabled = _tagNameToAdd.length > 0 ? TRUE : FALSE;
387287
if (enabled) {
388-
for (ProjectTag *lbl in _labels) {
389-
if ([lbl.name isEqualToString:_tempLabel]) {
288+
for (ProjectTag *lbl in _tagList) {
289+
if ([lbl.name isEqualToString:_tagNameToAdd]) {
390290
enabled = FALSE;
391291
break;
392292
}
@@ -407,7 +307,7 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField
407307
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
408308
{
409309
[super touchesBegan:touches withEvent:event];
410-
[_mCurrentTextField resignFirstResponder];
310+
[self.view endEditing:YES];
411311
}
412312

413313
@end

0 commit comments

Comments
 (0)