1515#import " TagsManager.h"
1616@interface SettingTagsViewController ()
1717@property (strong , nonatomic ) UICollectionView *tagsView;
18+ @property (strong , nonatomic ) NSMutableArray *mySelectedTags;
1819
1920@end
2021
@@ -30,7 +31,7 @@ + (instancetype)settingTagsVCWithAllTags:(NSArray *)allTags selectedTags:(NSArra
3031 if (selectedTags && selectedTags.count > 0 ) {
3132 vc.selectedTags = [NSMutableArray arrayWithArray: selectedTags];
3233 }else {
33- vc.selectedTags = [[ NSMutableArray alloc ] init ];
34+ vc.selectedTags = [NSMutableArray array ];
3435 }
3536 vc.doneBlock = block;
3637 return vc;
@@ -50,7 +51,18 @@ - (void)loadView{
5051 [super loadView ];
5152 CGRect frame = [UIView frameWithOutNav ];
5253 self.view = [[UIView alloc ] initWithFrame: frame];
53- self.navigationItem .rightBarButtonItem = [[UIBarButtonItem alloc ] initWithTitle: @" 完成" style: UIBarButtonItemStylePlain target: self action: @selector (doneBtnClicked: )];
54+
55+ _mySelectedTags = [_selectedTags mutableCopy ];
56+
57+ [self .navigationItem setRightBarButtonItem: [UIBarButtonItem itemWithBtnTitle: @" 完成" target: self action: @selector (doneBtnClicked: )] animated: YES ];
58+
59+ @weakify (self);
60+ RAC (self.navigationItem .rightBarButtonItem , enabled) =
61+ [RACSignal combineLatest: @[RACObserve (self , mySelectedTags)] reduce: ^id (NSString *mdStr){
62+ @strongify (self);
63+ return @(![self tagsHasChanged ]);
64+ }];
65+
5466 self.title = @" 个性标签" ;
5567 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc ] init ];
5668 self.tagsView = [[UICollectionView alloc ] initWithFrame: self .view.bounds collectionViewLayout: layout];
@@ -61,10 +73,17 @@ - (void)loadView{
6173 [self .view addSubview: self .tagsView];
6274}
6375
76+ - (BOOL )tagsHasChanged {
77+ BOOL tagsHasChanged = NO ;
78+ NSSet *oldSet = [NSSet setWithArray: _selectedTags], *newSet = [NSSet setWithArray: _mySelectedTags];
79+ tagsHasChanged = [newSet isEqualToSet: oldSet];
80+ return tagsHasChanged;
81+ }
82+
6483- (void )doneBtnClicked : (id )sender {
6584 [self .navigationController popViewControllerAnimated: YES ];
6685 if (self.doneBlock ) {
67- self.doneBlock (_selectedTags );
86+ self.doneBlock (_mySelectedTags );
6887 }
6988}
7089
@@ -78,7 +97,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
7897 TagCCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier: kCCellIdentifier_Tag forIndexPath: indexPath];
7998 Tag *curTag = [_allTags objectAtIndex: indexPath.row];
8099 ccell.curTag = curTag;
81- ccell.hasBeenSelected = [_selectedTags containsObject: curTag.id .stringValue];
100+ ccell.hasBeenSelected = [_mySelectedTags containsObject: curTag.id .stringValue];
82101 return ccell;
83102}
84103
@@ -99,10 +118,12 @@ - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectio
99118- (void )collectionView : (UICollectionView *)collectionView didSelectItemAtIndexPath : (NSIndexPath *)indexPath {
100119 Tag *curTag = [_allTags objectAtIndex: indexPath.row];
101120 NSString *tagId = curTag.id .stringValue ;
102- if ([_selectedTags containsObject: tagId]) {
103- [_selectedTags removeObject: tagId];
121+
122+ NSMutableArray *tempArray = [self mutableArrayValueForKey: @" mySelectedTags" ];
123+ if ([tempArray containsObject: tagId]) {
124+ [tempArray removeObject: tagId];
104125 }else {
105- [_selectedTags addObject: tagId];
126+ [tempArray addObject: tagId];
106127 }
107128 [collectionView reloadItemsAtIndexPaths: [NSArray arrayWithObject: indexPath]];
108129}
0 commit comments