Skip to content

Commit 41fa8b9

Browse files
wanghenghengEase
authored and
Ease
committed
本地文件批量编辑
1 parent cbd5f64 commit 41fa8b9

File tree

3 files changed

+215
-21
lines changed

3 files changed

+215
-21
lines changed

Coding_iOS/Controllers/LocalFilesViewController.m

+103-9
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#import "LocalFilesViewController.h"
1010
#import "LocalFileCell.h"
1111
#import "LocalFileViewController.h"
12+
#import "EaseToolBar.h"
1213

13-
@interface LocalFilesViewController ()<UITableViewDataSource, UITableViewDelegate, SWTableViewCellDelegate>
14+
@interface LocalFilesViewController ()<UITableViewDataSource, UITableViewDelegate, SWTableViewCellDelegate, EaseToolBarDelegate>
1415
@property (strong, nonatomic) UITableView *myTableView;
16+
@property (nonatomic, strong) EaseToolBar *myEditToolBar;
1517

1618
@end
1719

@@ -33,8 +35,10 @@ - (void)viewDidLoad{
3335
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
3436
make.edges.equalTo(self.view);
3537
}];
38+
tableView.allowsMultipleSelectionDuringEditing = YES;
3639
tableView;
3740
});
41+
[self changeEditStateToEditing:NO];
3842
}
3943

4044
#pragma mark T
@@ -55,13 +59,17 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
5559
}
5660

5761
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
58-
[tableView deselectRowAtIndexPath:indexPath animated:YES];
59-
60-
LocalFileViewController *vc = [LocalFileViewController new];
61-
vc.projectName = self.projectName;
62-
vc.fileUrl = self.fileList[indexPath.row];
63-
64-
[self.navigationController pushViewController:vc animated:YES];
62+
if (tableView.isEditing) {
63+
64+
}else{
65+
[tableView deselectRowAtIndexPath:indexPath animated:YES];
66+
67+
LocalFileViewController *vc = [LocalFileViewController new];
68+
vc.projectName = self.projectName;
69+
vc.fileUrl = self.fileList[indexPath.row];
70+
71+
[self.navigationController pushViewController:vc animated:YES];
72+
}
6573
}
6674

6775
#pragma mark SWTableViewCellDelegate
@@ -86,6 +94,92 @@ - (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityBut
8694
[actionSheet showInView:self.view];
8795
}
8896

97+
#pragma mark Edit
98+
- (void)changeEditStateToEditing:(BOOL)isEditing{
99+
[_myTableView setEditing:isEditing animated:YES];
100+
NSArray *rightBarButtonItems;
101+
if (isEditing) {
102+
UIBarButtonItem *item1 = [UIBarButtonItem itemWithBtnTitle:@"完成" target:self action:@selector(changeEditState)];
103+
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
104+
spaceItem.width = 20;
105+
UIBarButtonItem *item2 = [UIBarButtonItem itemWithBtnTitle:@"反选" target:self action:@selector(reverseSelect)];
106+
rightBarButtonItems = @[item1, spaceItem, item2];
107+
}else{
108+
UIBarButtonItem *item1 = [UIBarButtonItem itemWithBtnTitle:@"编辑" target:self action:@selector(changeEditState)];
109+
rightBarButtonItems = @[item1];
110+
}
111+
[self.navigationItem setRightBarButtonItems:rightBarButtonItems animated:YES];
112+
[self configToolBar];
113+
[self.myTableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.3];
114+
}
115+
- (void)changeEditState{
116+
[self changeEditStateToEditing:!_myTableView.isEditing];
117+
}
118+
119+
- (void)reverseSelect{
120+
if (_myTableView.isEditing) {
121+
NSArray *selectedIndexList = [_myTableView indexPathsForSelectedRows];
122+
NSMutableArray *reverseIndexList = [NSMutableArray new];
123+
for (NSInteger index = 0; index < _fileList.count; index++) {
124+
NSIndexPath *curIndex = [NSIndexPath indexPathForRow:index inSection:0];
125+
if (![selectedIndexList containsObject:curIndex]) {
126+
[reverseIndexList addObject:curIndex];
127+
}
128+
}
129+
for (NSIndexPath *indexPath in selectedIndexList) {
130+
[_myTableView deselectRowAtIndexPath:indexPath animated:YES];
131+
}
132+
for (NSIndexPath *indexPath in reverseIndexList) {
133+
[_myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
134+
}
135+
}
136+
}
137+
138+
- (void)configToolBar{
139+
//添加底部ToolBar
140+
if (!_myEditToolBar) {
141+
EaseToolBarItem *item = [EaseToolBarItem easeToolBarItemWithTitle:@" 删除" image:@"button_file_denete_enable" disableImage:nil];
142+
_myEditToolBar = [EaseToolBar easeToolBarWithItems:@[item]];
143+
_myEditToolBar.delegate = self;
144+
[self.view addSubview:_myEditToolBar];
145+
[_myEditToolBar mas_makeConstraints:^(MASConstraintMaker *make) {
146+
make.bottom.equalTo(self.view.mas_bottom);
147+
make.size.mas_equalTo(_myEditToolBar.frame.size);
148+
}];
149+
}
150+
_myEditToolBar.hidden = !_myTableView.isEditing;
151+
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0,_myTableView.isEditing? CGRectGetHeight(_myEditToolBar.frame): 0.0, 0.0);
152+
self.myTableView.contentInset = contentInsets;
153+
self.myTableView.scrollIndicatorInsets = contentInsets;
154+
}
155+
#pragma mark EaseToolBarDelegate
156+
- (void)easeToolBar:(EaseToolBar *)toolBar didClickedIndex:(NSInteger)index{
157+
NSArray *selectedIndexPath = [_myTableView indexPathsForSelectedRows];
158+
if (selectedIndexPath.count <= 0) {
159+
return;
160+
}
161+
if (toolBar == _myEditToolBar) {
162+
if (index == 0) {
163+
__weak typeof(self) weakSelf = self;
164+
UIActionSheet *actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle:@"确定要删除选中的本地文件吗?" buttonTitles:nil destructiveTitle:@"确认删除" cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
165+
if (index == 0) {
166+
[weakSelf deleteSelectedFiles];
167+
}
168+
}];
169+
[actionSheet showInView:self.view];
170+
}
171+
}
172+
}
173+
174+
- (void)deleteSelectedFiles{
175+
NSArray *selectedIndexPath = [_myTableView indexPathsForSelectedRows];
176+
NSMutableArray *selectedFiles = [[NSMutableArray alloc] initWithCapacity:selectedIndexPath.count];
177+
for (NSIndexPath *indexPath in selectedIndexPath) {
178+
[selectedFiles addObject:_fileList[indexPath.row]];
179+
}
180+
[self deleteFilesWithUrlList:selectedFiles];
181+
}
182+
89183
#pragma mark Delete
90184
- (void)deleteFilesWithUrlList:(NSArray *)urlList{
91185
@weakify(self);
@@ -104,7 +198,7 @@ - (void)deleteFilesWithUrlList:(NSArray *)urlList{
104198
dispatch_async(dispatch_get_main_queue(), ^{
105199
@strongify(self);
106200
[self.fileList removeObjectsInArray:urlList];
107-
[self.myTableView reloadData];
201+
[self changeEditStateToEditing:NO];
108202
[NSObject showHudTipStr:@"本地文件删除成功"];
109203
});
110204
});

Coding_iOS/Controllers/LocalFoldersViewController.m

+110-11
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
#import "Coding_NetAPIManager.h"
1313
#import "LocalFolderCell.h"
1414
#import "LocalFilesViewController.h"
15+
#import "EaseToolBar.h"
1516

16-
@interface LocalFoldersViewController ()<UITableViewDataSource, UITableViewDelegate>
17+
@interface LocalFoldersViewController ()<UITableViewDataSource, UITableViewDelegate, SWTableViewCellDelegate, EaseToolBarDelegate>
1718
@property (assign, nonatomic) BOOL isLoading;
1819
@property (strong, nonatomic) UITableView *myTableView;
1920
@property (strong, nonatomic) ODRefreshControl *myRefreshControl;
2021

22+
@property (nonatomic, strong) EaseToolBar *myEditToolBar;
23+
2124
@property (strong, nonatomic) NSMutableArray *projectId_list;
2225
@property (strong, nonatomic) NSMutableDictionary *projectList_dict;
2326
@end
@@ -40,6 +43,7 @@ - (void)viewDidLoad{
4043
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
4144
make.edges.equalTo(self.view);
4245
}];
46+
tableView.allowsMultipleSelectionDuringEditing = YES;
4347
tableView;
4448
});
4549
_myRefreshControl = [[ODRefreshControl alloc] initInScrollView:self.myTableView];
@@ -48,6 +52,7 @@ - (void)viewDidLoad{
4852

4953
- (void)viewWillAppear:(BOOL)animated{
5054
[super viewWillAppear:animated];
55+
[self changeEditStateToEditing:NO];
5156
[self refresh];
5257
}
5358

@@ -144,16 +149,20 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
144149
}
145150

146151
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
147-
[tableView deselectRowAtIndexPath:indexPath animated:YES];
148-
149-
NSString *key = _projectId_list[indexPath.row];
150-
NSDictionary *pro_dict = _projectList_dict[key];
151-
152-
LocalFilesViewController *vc = [LocalFilesViewController new];
153-
vc.projectName = pro_dict[@"name"];
154-
vc.fileList = pro_dict[@"list"];
155-
156-
[self.navigationController pushViewController:vc animated:YES];
152+
if (tableView.isEditing) {
153+
154+
}else{
155+
[tableView deselectRowAtIndexPath:indexPath animated:YES];
156+
157+
NSString *key = _projectId_list[indexPath.row];
158+
NSDictionary *pro_dict = _projectList_dict[key];
159+
160+
LocalFilesViewController *vc = [LocalFilesViewController new];
161+
vc.projectName = pro_dict[@"name"];
162+
vc.fileList = pro_dict[@"list"];
163+
164+
[self.navigationController pushViewController:vc animated:YES];
165+
}
157166
}
158167

159168
#pragma mark SWTableViewCellDelegate
@@ -179,6 +188,95 @@ - (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityBut
179188
[actionSheet showInView:self.view];
180189
}
181190

191+
#pragma mark Edit
192+
- (void)changeEditStateToEditing:(BOOL)isEditing{
193+
[_myTableView setEditing:isEditing animated:YES];
194+
NSArray *rightBarButtonItems;
195+
if (isEditing) {
196+
UIBarButtonItem *item1 = [UIBarButtonItem itemWithBtnTitle:@"完成" target:self action:@selector(changeEditState)];
197+
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
198+
spaceItem.width = 20;
199+
UIBarButtonItem *item2 = [UIBarButtonItem itemWithBtnTitle:@"反选" target:self action:@selector(reverseSelect)];
200+
rightBarButtonItems = @[item1, spaceItem, item2];
201+
}else{
202+
UIBarButtonItem *item1 = [UIBarButtonItem itemWithBtnTitle:@"编辑" target:self action:@selector(changeEditState)];
203+
rightBarButtonItems = @[item1];
204+
}
205+
[self.navigationItem setRightBarButtonItems:rightBarButtonItems animated:YES];
206+
[self configToolBar];
207+
[self.myTableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.3];
208+
}
209+
- (void)changeEditState{
210+
[self changeEditStateToEditing:!_myTableView.isEditing];
211+
}
212+
213+
- (void)reverseSelect{
214+
if (_myTableView.isEditing) {
215+
NSArray *selectedIndexList = [_myTableView indexPathsForSelectedRows];
216+
NSMutableArray *reverseIndexList = [NSMutableArray new];
217+
for (NSInteger index = 0; index < _projectId_list.count; index++) {
218+
NSIndexPath *curIndex = [NSIndexPath indexPathForRow:index inSection:0];
219+
if (![selectedIndexList containsObject:curIndex]) {
220+
[reverseIndexList addObject:curIndex];
221+
}
222+
}
223+
for (NSIndexPath *indexPath in selectedIndexList) {
224+
[_myTableView deselectRowAtIndexPath:indexPath animated:YES];
225+
}
226+
for (NSIndexPath *indexPath in reverseIndexList) {
227+
[_myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
228+
}
229+
}
230+
}
231+
232+
- (void)configToolBar{
233+
//添加底部ToolBar
234+
if (!_myEditToolBar) {
235+
EaseToolBarItem *item = [EaseToolBarItem easeToolBarItemWithTitle:@" 删除" image:@"button_file_denete_enable" disableImage:nil];
236+
_myEditToolBar = [EaseToolBar easeToolBarWithItems:@[item]];
237+
_myEditToolBar.delegate = self;
238+
[self.view addSubview:_myEditToolBar];
239+
[_myEditToolBar mas_makeConstraints:^(MASConstraintMaker *make) {
240+
make.bottom.equalTo(self.view.mas_bottom);
241+
make.size.mas_equalTo(_myEditToolBar.frame.size);
242+
}];
243+
}
244+
_myEditToolBar.hidden = !_myTableView.isEditing;
245+
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0,_myTableView.isEditing? CGRectGetHeight(_myEditToolBar.frame): 0.0, 0.0);
246+
self.myTableView.contentInset = contentInsets;
247+
self.myTableView.scrollIndicatorInsets = contentInsets;
248+
}
249+
#pragma mark EaseToolBarDelegate
250+
- (void)easeToolBar:(EaseToolBar *)toolBar didClickedIndex:(NSInteger)index{
251+
NSArray *selectedIndexPath = [_myTableView indexPathsForSelectedRows];
252+
if (selectedIndexPath.count <= 0) {
253+
return;
254+
}
255+
if (toolBar == _myEditToolBar) {
256+
if (index == 0) {
257+
__weak typeof(self) weakSelf = self;
258+
UIActionSheet *actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle:@"确定要删除选中文件夹内所有本地文件吗?" buttonTitles:nil destructiveTitle:@"确认删除" cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
259+
if (index == 0) {
260+
[weakSelf deleteSelectedFolders];
261+
}
262+
}];
263+
[actionSheet showInView:self.view];
264+
}
265+
}
266+
}
267+
268+
- (void)deleteSelectedFolders{
269+
NSArray *selectedIndexPath = [_myTableView indexPathsForSelectedRows];
270+
NSMutableArray *selectedFolders = [[NSMutableArray alloc] initWithCapacity:selectedIndexPath.count];
271+
for (NSIndexPath *indexPath in selectedIndexPath) {
272+
NSString *projectId = _projectId_list[indexPath.row];
273+
if (projectId.length > 0) {
274+
[selectedFolders addObject:projectId];
275+
}
276+
}
277+
[self deleteFilesWithProjectIdList:selectedFolders];
278+
}
279+
182280
#pragma mark Delete
183281
- (void)deleteFilesWithProjectIdList:(NSArray *)projectIdList{
184282
NSMutableArray *urlList = [NSMutableArray new];
@@ -209,6 +307,7 @@ - (void)deleteFilesWithUrlList:(NSArray *)urlList{
209307
dispatch_async(dispatch_get_main_queue(), ^{
210308
@strongify(self);
211309
[self findLocalFile];
310+
[self changeEditStateToEditing:NO];
212311
[NSObject showHudTipStr:@"本地文件删除成功"];
213312
});
214313
});

Coding_iOS/Views/Cell/LocalFileCell.m

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
2525
CGFloat icon_width = 45.0;
2626
if (!_iconView) {
2727
_iconView = [UIImageView new];
28+
_iconView.contentMode = UIViewContentModeScaleAspectFill;
2829
_iconView.layer.masksToBounds = YES;
2930
_iconView.layer.cornerRadius = 2.0;
3031
_iconView.layer.borderWidth = 0.5;
@@ -72,7 +73,7 @@ - (void)setFileUrl:(NSURL *)fileUrl{
7273
}
7374
if (image) {
7475
CGFloat icon_width = 2 * 45.0;
75-
image = [image scaleToSize:CGSizeMake(icon_width, icon_width) usingMode:NYXResizeModeScaleToFill];
76+
image = [image scaleToSize:CGSizeMake(icon_width, icon_width) usingMode:NYXResizeModeAspectFill];
7677
}else{
7778
image = [UIImage imageWithFileType:fileType];
7879
}

0 commit comments

Comments
 (0)