Skip to content

Commit cbd5f64

Browse files
wanghenghengEase
authored and
Ease
committed
文件&文件夹 滑动删除
1 parent 1d4055e commit cbd5f64

File tree

4 files changed

+120
-4
lines changed

4 files changed

+120
-4
lines changed

Coding_iOS/Controllers/LocalFilesViewController.m

+48-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import "LocalFileCell.h"
1111
#import "LocalFileViewController.h"
1212

13-
@interface LocalFilesViewController ()<UITableViewDataSource, UITableViewDelegate>
13+
@interface LocalFilesViewController ()<UITableViewDataSource, UITableViewDelegate, SWTableViewCellDelegate>
1414
@property (strong, nonatomic) UITableView *myTableView;
1515

1616
@end
@@ -45,6 +45,7 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
4545
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
4646
LocalFileCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LocalFileCell forIndexPath:indexPath];
4747
cell.fileUrl = _fileList[indexPath.row];
48+
cell.delegate = self;
4849
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
4950
return cell;
5051
}
@@ -62,4 +63,50 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
6263

6364
[self.navigationController pushViewController:vc animated:YES];
6465
}
66+
67+
#pragma mark SWTableViewCellDelegate
68+
- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell{
69+
return YES;
70+
}
71+
- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state{
72+
return YES;
73+
}
74+
75+
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index {
76+
[cell hideUtilityButtonsAnimated:YES];
77+
78+
NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell];
79+
NSURL *fileUrl = self.fileList[indexPath.row];
80+
__weak typeof(self) weakSelf = self;
81+
UIActionSheet *actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle:@"确定要删除本地文件吗?" buttonTitles:nil destructiveTitle:@"确认删除" cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
82+
if (index == 0) {
83+
[weakSelf deleteFilesWithUrlList:@[fileUrl]];
84+
}
85+
}];
86+
[actionSheet showInView:self.view];
87+
}
88+
89+
#pragma mark Delete
90+
- (void)deleteFilesWithUrlList:(NSArray *)urlList{
91+
@weakify(self);
92+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
93+
NSFileManager *fm = [NSFileManager defaultManager];
94+
for (NSURL *fileUrl in urlList) {
95+
NSString *filePath = fileUrl.path;
96+
if ([fm fileExistsAtPath:filePath]) {
97+
NSError *fileError;
98+
[fm removeItemAtPath:filePath error:&fileError];
99+
if (fileError) {
100+
[NSObject showError:fileError];
101+
}
102+
}
103+
}
104+
dispatch_async(dispatch_get_main_queue(), ^{
105+
@strongify(self);
106+
[self.fileList removeObjectsInArray:urlList];
107+
[self.myTableView reloadData];
108+
[NSObject showHudTipStr:@"本地文件删除成功"];
109+
});
110+
});
111+
}
65112
@end

Coding_iOS/Controllers/LocalFoldersViewController.m

+65-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ - (void)viewDidLoad{
4444
});
4545
_myRefreshControl = [[ODRefreshControl alloc] initInScrollView:self.myTableView];
4646
[_myRefreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
47+
}
48+
49+
- (void)viewWillAppear:(BOOL)animated{
50+
[super viewWillAppear:animated];
4751
[self refresh];
4852
}
4953

@@ -100,9 +104,9 @@ - (BOOL)findLocalFile{
100104
[pro_dict[@"list"] addObject:fileUrl];
101105
}
102106
}
103-
return YES;
104107
}
105-
return NO;
108+
[self.myTableView reloadData];
109+
return localFileUrlList.count > 0;
106110
}
107111

108112
- (void)refreshProjectNameWithProjects:(Projects *)projects{
@@ -130,7 +134,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
130134
NSDictionary *pro_dict = _projectList_dict[key];
131135

132136
[cell setProjectName:pro_dict[@"name"] fileCount:[(NSArray *)pro_dict[@"list"] count]];
133-
137+
cell.delegate = self;
134138
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
135139
return cell;
136140
}
@@ -151,4 +155,62 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
151155

152156
[self.navigationController pushViewController:vc animated:YES];
153157
}
158+
159+
#pragma mark SWTableViewCellDelegate
160+
- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell{
161+
return YES;
162+
}
163+
- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state{
164+
return YES;
165+
}
166+
167+
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index {
168+
[cell hideUtilityButtonsAnimated:YES];
169+
170+
NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell];
171+
NSString *projectId = _projectId_list[indexPath.row];
172+
173+
__weak typeof(self) weakSelf = self;
174+
UIActionSheet *actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle:@"确定要删除该文件夹内所有本地文件吗?" buttonTitles:nil destructiveTitle:@"确认删除" cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
175+
if (index == 0) {
176+
[weakSelf deleteFilesWithProjectIdList:@[projectId]];
177+
}
178+
}];
179+
[actionSheet showInView:self.view];
180+
}
181+
182+
#pragma mark Delete
183+
- (void)deleteFilesWithProjectIdList:(NSArray *)projectIdList{
184+
NSMutableArray *urlList = [NSMutableArray new];
185+
for (NSString *key in projectIdList) {
186+
NSDictionary *pro_dict = _projectList_dict[key];
187+
NSArray *curList = pro_dict[@"list"];
188+
if (curList.count > 0) {
189+
[urlList addObjectsFromArray:curList];
190+
}
191+
}
192+
[self deleteFilesWithUrlList:urlList];
193+
}
194+
195+
- (void)deleteFilesWithUrlList:(NSArray *)urlList{
196+
@weakify(self);
197+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
198+
NSFileManager *fm = [NSFileManager defaultManager];
199+
for (NSURL *fileUrl in urlList) {
200+
NSString *filePath = fileUrl.path;
201+
if ([fm fileExistsAtPath:filePath]) {
202+
NSError *fileError;
203+
[fm removeItemAtPath:filePath error:&fileError];
204+
if (fileError) {
205+
[NSObject showError:fileError];
206+
}
207+
}
208+
}
209+
dispatch_async(dispatch_get_main_queue(), ^{
210+
@strongify(self);
211+
[self findLocalFile];
212+
[NSObject showHudTipStr:@"本地文件删除成功"];
213+
});
214+
});
215+
}
154216
@end

Coding_iOS/Views/Cell/LocalFileCell.m

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
4848
make.centerY.equalTo(self.contentView);
4949
make.height.mas_equalTo(30);
5050
}];
51+
52+
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
53+
[rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithHexString:@"0xff5846"] icon:[UIImage imageNamed:@"icon_file_cell_delete"]];
54+
[self setRightUtilityButtons:rightUtilityButtons WithButtonWidth:[LocalFileCell cellHeight]];
5155
}
5256
return self;
5357
}

Coding_iOS/Views/Cell/LocalFolderCell.m

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
4343
make.centerY.equalTo(self.contentView);
4444
make.height.mas_equalTo(30);
4545
}];
46+
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
47+
[rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithHexString:@"0xff5846"] icon:[UIImage imageNamed:@"icon_file_cell_delete"]];
48+
[self setRightUtilityButtons:rightUtilityButtons WithButtonWidth:[LocalFolderCell cellHeight]];
4649
}
4750
return self;
4851
}

0 commit comments

Comments
 (0)