未验证 提交 0b931a0f 编写于 作者: D Dilrvvr 提交者: GitHub

fix(iOS): UserDefaults search (#804)

* fix(iOS): NSUserDefaults 搜索结果点击后展示错误 & 背景色适配

* fix(iOS): 深色模式导航条返回按钮未自动切换的问题

* feat(iOS): 沙盒文件排序
Co-authored-by: Nalbert <jkdev123cool@gmail.com>
上级 3a6b1309
......@@ -17,6 +17,7 @@
@property (nonatomic, strong) DoraemonNavBarItemModel *leftModel;
@property (nonatomic, strong) NSArray *leftNavBarItemArray;
@end
@implementation DoraemonBaseViewController
......@@ -85,6 +86,9 @@
} else {
self.leftModel.image = [UIImage doraemon_xcassetImageNamed:@"doraemon_back"];
}
if (self.leftNavBarItemArray) {
[self setLeftNavBarItems:self.leftNavBarItemArray];
}
}
}
#endif
......@@ -113,6 +117,7 @@
}
- (void)setLeftNavBarItems:(NSArray *)items{
_leftNavBarItemArray = items;
NSArray *barItems = [self navigationItems:items];
if (barItems) {
self.navigationItem.leftBarButtonItems = barItems;
......
......@@ -11,11 +11,16 @@
#import "DoraemonNSUserDefaultsEditViewController.h"
@interface DoraemonNSUserDefaultsViewController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<DoraemonNSUserDefaultsModel *> *modelList;
@property (nonatomic, weak) UITextField *searchTextField;
@property (nonatomic, strong) NSMutableArray<DoraemonNSUserDefaultsModel *> *searchList;
@property (nonatomic, strong, readonly) NSMutableArray<DoraemonNSUserDefaultsModel *> *dataArray;
@property (nonatomic, assign) BOOL isSearch;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, weak) UITextField *searchTextField;
@property (nonatomic, strong) UIBarButtonItem *clearAllItem;
@property (nonatomic, strong) UIBarButtonItem *cancelItem;
@end
......@@ -29,9 +34,14 @@
[self buildSearchUI];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, IPHONE_NAVIGATIONBAR_HEIGHT, self.view.doraemon_width, self.view.doraemon_height-IPHONE_NAVIGATIONBAR_HEIGHT) style:UITableViewStylePlain];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, IPHONE_NAVIGATIONBAR_HEIGHT, self.view.doraemon_width, self.view.doraemon_height - IPHONE_NAVIGATIONBAR_HEIGHT) style:UITableViewStylePlain];
if (@available(iOS 13.0, *)) {
self.tableView.backgroundColor = [UIColor systemBackgroundColor];
} else {
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}
// 该方式退出键盘(系统键盘)时会在底部卡顿一下
//self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
......@@ -59,7 +69,7 @@
searchTextField.rightView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 10.0, 36.0)];
searchTextField.leftViewMode = UITextFieldViewModeAlways;
searchTextField.rightViewMode = UITextFieldViewModeAlways;
searchTextField.rightViewMode = UITextFieldViewModeUnlessEditing;
}
- (void)viewWillAppear:(BOOL)animated {
......@@ -90,41 +100,6 @@
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.isSearch ? self.searchList.count : self.modelList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifer = @"DoraemonNSUserDefaultsViewControllerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
}
DoraemonNSUserDefaultsModel *model = self.isSearch ? self.searchList[indexPath.row] : self.modelList[indexPath.row];
cell.textLabel.text = model.key;
cell.detailTextLabel.text = [model.value description];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
DoraemonNSUserDefaultsModel *model = self.isSearch ? _searchList[indexPath.row] : _modelList[indexPath.row];
[[NSUserDefaults standardUserDefaults] setValue:nil forKey:model.key];
[self reload];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DoraemonNSUserDefaultsModel *model = self.modelList[indexPath.row];
DoraemonNSUserDefaultsEditViewController *vc = [[DoraemonNSUserDefaultsEditViewController alloc] initWithModel:model];
[self.navigationController pushViewController:vc animated:true];
}
- (void)clearUserDefaults {
if ([self.searchTextField isFirstResponder]) {
......@@ -197,6 +172,52 @@
[self.tableView reloadData];
}
#pragma mark
#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifer = @"DoraemonNSUserDefaultsViewControllerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
}
DoraemonNSUserDefaultsModel *model = self.dataArray[indexPath.row];
cell.textLabel.text = model.key;
cell.detailTextLabel.text = [model.value description];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
DoraemonNSUserDefaultsModel *model = self.dataArray[indexPath.row];
[[NSUserDefaults standardUserDefaults] setValue:nil forKey:model.key];
[self reload];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DoraemonNSUserDefaultsModel *model = self.dataArray[indexPath.row];
DoraemonNSUserDefaultsEditViewController *vc = [[DoraemonNSUserDefaultsEditViewController alloc] initWithModel:model];
[self.navigationController pushViewController:vc animated:true];
}
#pragma mark
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self.searchTextField resignFirstResponder];
}
#pragma mark
#pragma mark - UITextFieldDelegate
......@@ -219,6 +240,11 @@
#pragma mark
#pragma mark - Property
- (NSMutableArray<DoraemonNSUserDefaultsModel *> *)dataArray {
return self.isSearch ? self.searchList : self.modelList;
}
- (NSMutableArray<DoraemonNSUserDefaultsModel *> *)searchList {
if (!_searchList) {
_searchList = [NSMutableArray array];
......
......@@ -130,7 +130,34 @@
[files addObject:model];
}
_dataArray = files.copy;
//_dataArray = files.copy;
// 按名称排序,并保持文件夹在上
_dataArray = [files sortedArrayUsingComparator:^NSComparisonResult(DoraemonSandboxModel * _Nonnull obj1, DoraemonSandboxModel * _Nonnull obj2) {
BOOL isObj1Directory = (obj1.type == DoraemonSandboxFileTypeDirectory);
BOOL isObj2Directory = (obj2.type == DoraemonSandboxFileTypeDirectory);
// 都是目录 或 都不是目录
BOOL isSameType = ((isObj1Directory && isObj2Directory) || (!isObj1Directory && !isObj2Directory));
if (isSameType) { // 都是目录 或 都不是目录
// 按名称排序
return [obj1.name.lowercaseString compare:obj2.name.lowercaseString];
}
// 以下是一个为目录,一个不为目录的情况
if (isObj1Directory) { // obj1是目录
// 升序,保持文件夹在上
return NSOrderedAscending;
}
// obj2是目录,降序
return NSOrderedDescending;
}];
[self.tableView reloadData];
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册