|
|
@@ -0,0 +1,299 @@
|
|
|
+//
|
|
|
+// EnumSelectAndSortViewController.m
|
|
|
+// enumsort
|
|
|
+//
|
|
|
+// Created by Jack on 2017/10/26.
|
|
|
+// Copyright © 2017年 Jack. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+#import "EnumSelectAndSortViewController.h"
|
|
|
+#import "EnumSelectAndSortCell.h"
|
|
|
+#import "EnumModel.h"
|
|
|
+
|
|
|
+@interface EnumSelectAndSortViewController () <UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
|
|
|
+
|
|
|
+@property (nonatomic,strong) NSDictionary *dicDataSource;
|
|
|
+@property (nonatomic,strong) NSMutableArray<EnumModel *> *arrayDataSource;
|
|
|
+@property (nonatomic,strong) NSMutableArray<EnumModel *> *modelArray;
|
|
|
+
|
|
|
+@property (nonatomic,copy) NSString *searchKeyword;
|
|
|
+@property (nonatomic,assign) BOOL searching;
|
|
|
+@property (nonatomic,strong) UITapGestureRecognizer *tapGesture;
|
|
|
+
|
|
|
+@property (weak, nonatomic) IBOutlet UITableView *enumTableView;
|
|
|
+@property (weak, nonatomic) IBOutlet UISearchBar *enumSearchBar;
|
|
|
+@property (weak, nonatomic) IBOutlet NSLayoutConstraint *table_bottom_constraint;
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation EnumSelectAndSortViewController
|
|
|
+
|
|
|
+- (void)viewDidLoad {
|
|
|
+ [super viewDidLoad];
|
|
|
+ // Do any additional setup after loading the view.
|
|
|
+
|
|
|
+ self.enumTableView.allowsSelectionDuringEditing = YES;
|
|
|
+ UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressTableViewToEdit:)];
|
|
|
+ [self.enumTableView addGestureRecognizer:gesture];
|
|
|
+
|
|
|
+ [self setupNavigationBar];
|
|
|
+ [self registerKeyboardNotification];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)didReceiveMemoryWarning {
|
|
|
+ [super didReceiveMemoryWarning];
|
|
|
+ // Dispose of any resources that can be recreated.
|
|
|
+}
|
|
|
+
|
|
|
+- (void)dealloc {
|
|
|
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Keyboard Notification
|
|
|
+
|
|
|
+- (void)registerKeyboardNotification {
|
|
|
+
|
|
|
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChangeFrameNotificatoin:) name:UIKeyboardWillChangeFrameNotification object:nil];
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)keyboardChangeFrameNotificatoin:(NSNotification *)notification {
|
|
|
+ NSTimeInterval duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
|
|
|
+// CGRect begin = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
|
|
|
+ CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
|
|
|
+
|
|
|
+ [UIView animateWithDuration:duration animations:^{
|
|
|
+ self.table_bottom_constraint.constant = self.view.bounds.size.height - end.origin.y;
|
|
|
+ }];
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - UI
|
|
|
+
|
|
|
+- (void)setupNavigationBar {
|
|
|
+
|
|
|
+ if (self.navigationController && !self.navigationController.navigationBarHidden) {
|
|
|
+ UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
|
|
|
+ style:UIBarButtonItemStylePlain
|
|
|
+ target:self
|
|
|
+ action:@selector(leftBarItemClick:)];
|
|
|
+ self.navigationItem.leftBarButtonItem = leftItem;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Data
|
|
|
+
|
|
|
+- (void)setEnumDataSource:(NSDictionary *)dataSource {
|
|
|
+ _dicDataSource = dataSource;
|
|
|
+
|
|
|
+ if (!_arrayDataSource) {
|
|
|
+ _arrayDataSource = [NSMutableArray array];
|
|
|
+ } else {
|
|
|
+ [_arrayDataSource removeAllObjects];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!_modelArray) {
|
|
|
+ _modelArray = [NSMutableArray array];
|
|
|
+ } else {
|
|
|
+ [_modelArray removeAllObjects];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (dataSource) {
|
|
|
+ for (int i=0; i<[[_dicDataSource valueForKey:@"count"] intValue]; i++)
|
|
|
+ {
|
|
|
+ NSDictionary* val_json = [_dicDataSource objectForKey:[NSString stringWithFormat:@"val_%d",i ]];
|
|
|
+ EnumModel *model = [[EnumModel alloc] init];
|
|
|
+ [model setJsonValue:val_json forSortId:i];
|
|
|
+ [_modelArray addObject:model];
|
|
|
+ }
|
|
|
+ [_arrayDataSource addObjectsFromArray:_modelArray];
|
|
|
+ }
|
|
|
+
|
|
|
+ [self.enumTableView reloadData];
|
|
|
+}
|
|
|
+
|
|
|
+- (NSDictionary *)resultDataDic {
|
|
|
+ NSMutableDictionary *dic = [NSMutableDictionary dictionary];
|
|
|
+
|
|
|
+ for (int i = 0; i < _arrayDataSource.count; i++) {
|
|
|
+ EnumModel *model = [_arrayDataSource objectAtIndex:i];
|
|
|
+ NSDictionary *val_json = @{
|
|
|
+ @"check": @(model.check),
|
|
|
+ @"value": model.value,
|
|
|
+ @"value_id": @(model.value_id)
|
|
|
+ };
|
|
|
+ NSString *key = [NSString stringWithFormat:@"val_%d",i];
|
|
|
+ [dic setObject:val_json forKey:key];
|
|
|
+ }
|
|
|
+
|
|
|
+ return dic;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)searchKeyword:(NSString *)searchKeyword {
|
|
|
+ self.searchKeyword = searchKeyword;
|
|
|
+
|
|
|
+ [_arrayDataSource removeAllObjects];
|
|
|
+ if (searchKeyword != nil && searchKeyword.length > 0) {
|
|
|
+
|
|
|
+
|
|
|
+ [_modelArray enumerateObjectsUsingBlock:^(EnumModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
|
+
|
|
|
+ EnumModel *model = (EnumModel *)obj;
|
|
|
+ if ([model.value.lowercaseString containsString:searchKeyword.lowercaseString]) {
|
|
|
+ [_arrayDataSource addObject:model];
|
|
|
+ }
|
|
|
+
|
|
|
+ }];
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ [_arrayDataSource addObjectsFromArray:_modelArray];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ [_arrayDataSource sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
|
|
|
+
|
|
|
+ EnumModel *model1 = (EnumModel *)obj1;
|
|
|
+ EnumModel *model2 = (EnumModel *)obj2;
|
|
|
+
|
|
|
+ if (model1.sort_id > model2.sort_id) {
|
|
|
+ return NSOrderedDescending;
|
|
|
+ } else {
|
|
|
+ return NSOrderedAscending;
|
|
|
+ }
|
|
|
+
|
|
|
+ }];
|
|
|
+
|
|
|
+ [self.enumTableView reloadData];
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Gesture
|
|
|
+
|
|
|
+- (UITapGestureRecognizer *)tapGesture {
|
|
|
+ if (!_tapGesture) {
|
|
|
+ _tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToEndSearch:)];
|
|
|
+ }
|
|
|
+ return _tapGesture;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)longPressTableViewToEdit:(UILongPressGestureRecognizer *)gesture {
|
|
|
+ [self.enumTableView setEditing:YES animated:YES];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)tapToEndSearch:(UITapGestureRecognizer *)gesture {
|
|
|
+ [self.enumSearchBar endEditing:YES];
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - TableView DataSource & Delegate
|
|
|
+
|
|
|
+- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
|
+ return _arrayDataSource.count;
|
|
|
+}
|
|
|
+
|
|
|
+- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
|
|
|
+
|
|
|
+ EnumSelectAndSortCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EnumSelectAndSortCell" forIndexPath:indexPath];
|
|
|
+ cell.selectionStyle = UITableViewCellSelectionStyleNone;
|
|
|
+
|
|
|
+ EnumModel *model = [_arrayDataSource objectAtIndex:indexPath.row];
|
|
|
+
|
|
|
+ [cell setEnumValue:model.value];
|
|
|
+ [cell setEnumSelection:model.check];
|
|
|
+
|
|
|
+ return cell;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ if (!self.searching) {
|
|
|
+
|
|
|
+ if (tableView.isEditing) {
|
|
|
+ [tableView setEditing:NO animated:YES];
|
|
|
+ } else {
|
|
|
+ EnumSelectAndSortCell *cell = [tableView cellForRowAtIndexPath:indexPath];
|
|
|
+
|
|
|
+ EnumModel *model = [_arrayDataSource objectAtIndex:indexPath.row];
|
|
|
+ model.check = !model.check;
|
|
|
+
|
|
|
+ [cell setEnumSelection:model.check];
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ return 50.f;
|
|
|
+}
|
|
|
+
|
|
|
+- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ return YES;
|
|
|
+}
|
|
|
+
|
|
|
+- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ return UITableViewCellEditingStyleNone;
|
|
|
+}
|
|
|
+
|
|
|
+-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
|
|
|
+
|
|
|
+ EnumModel *source_model = [_arrayDataSource objectAtIndex:sourceIndexPath.row];
|
|
|
+
|
|
|
+ // 修改数据源中的排序
|
|
|
+ if (sourceIndexPath.row > destinationIndexPath.row) {
|
|
|
+ [_arrayDataSource removeObject:source_model];
|
|
|
+ [_arrayDataSource insertObject:source_model atIndex:destinationIndexPath.row];
|
|
|
+ } else {
|
|
|
+ [_arrayDataSource removeObject:source_model];
|
|
|
+ [_arrayDataSource insertObject:source_model atIndex:destinationIndexPath.row - 1];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 修改Model Sort Id
|
|
|
+ [_arrayDataSource enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
|
+
|
|
|
+ EnumModel *model = (EnumModel *)obj;
|
|
|
+ model.sort_id = idx;
|
|
|
+
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Action
|
|
|
+
|
|
|
+- (void)leftBarItemClick:(UIBarButtonItem *)sender {
|
|
|
+
|
|
|
+ if (self.returnBlock && self.indexPath) {
|
|
|
+ self.returnBlock([self resultDataDic], self.indexPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ [self.navigationController popViewControllerAnimated:YES];
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - SearchBar Delegate
|
|
|
+
|
|
|
+- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
|
|
|
+ [self searchKeyword:searchText];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
|
|
|
+ [self searchKeyword:searchBar.text];
|
|
|
+}
|
|
|
+
|
|
|
+- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
|
|
|
+ self.searching = YES;
|
|
|
+ [self.enumTableView addGestureRecognizer:self.tapGesture];
|
|
|
+ return YES;
|
|
|
+}
|
|
|
+
|
|
|
+- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
|
|
|
+ self.searching = NO;
|
|
|
+ [searchBar resignFirstResponder];
|
|
|
+ [self.enumTableView removeGestureRecognizer:self.tapGesture];
|
|
|
+ return YES;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+@end
|