فهرست منبع

1.修改iOS Apex Mobile Search,增加搜索框。

Pen Li 8 سال پیش
والد
کامیت
a08fce377f
2فایلهای تغییر یافته به همراه122 افزوده شده و 21 حذف شده
  1. 48 5
      Apex Mobile/Apex Mobile/Main.storyboard
  2. 74 16
      Apex Mobile/Apex Mobile/SearchViewController.m

+ 48 - 5
Apex Mobile/Apex Mobile/Main.storyboard

@@ -827,9 +827,8 @@
                         <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                         <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                         <subviews>
-                            <tableView clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" allowsSelection="NO" rowHeight="82" sectionHeaderHeight="22" sectionFooterHeight="42" translatesAutoresizingMaskIntoConstraints="NO" id="ppj-4o-rzu">
-                                <rect key="frame" x="0.0" y="0.0" width="375" height="623"/>
-                                <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
+                            <tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" allowsSelection="NO" rowHeight="82" sectionHeaderHeight="22" sectionFooterHeight="42" translatesAutoresizingMaskIntoConstraints="NO" id="ppj-4o-rzu">
+                                <rect key="frame" x="0.0" y="76" width="375" height="547"/>
                                 <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                 <prototypes>
                                     <tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="item_date" rowHeight="82" id="mTz-zv-fyY" customClass="TableCellDate">
@@ -967,9 +966,8 @@
                                     <outlet property="delegate" destination="C3t-ED-9dx" id="ZHb-4L-dA1"/>
                                 </connections>
                             </tableView>
-                            <toolbar opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="FYC-YO-04g">
+                            <toolbar opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="FYC-YO-04g">
                                 <rect key="frame" x="0.0" y="623" width="375" height="44"/>
-                                <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
                                 <items>
                                     <barButtonItem style="plain" id="bX6-SA-fCQ">
                                         <button key="customView" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" id="q98-Ul-Lg7">
@@ -998,8 +996,51 @@
                                     </barButtonItem>
                                 </items>
                             </toolbar>
+                            <toolbar opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="s08-SO-Hkg">
+                                <rect key="frame" x="0.0" y="20" width="375" height="56"/>
+                                <constraints>
+                                    <constraint firstAttribute="height" constant="56" id="l4v-hS-UAY"/>
+                                </constraints>
+                                <items>
+                                    <barButtonItem style="plain" id="yjo-8W-d3C">
+                                        <searchBar key="customView" contentMode="redraw" text="" id="NyC-qf-cxA">
+                                            <rect key="frame" x="16" y="0.0" width="297" height="56"/>
+                                            <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
+                                            <textInputTraits key="textInputTraits"/>
+                                        </searchBar>
+                                    </barButtonItem>
+                                    <barButtonItem width="10" style="plain" systemItem="fixedSpace" id="0kh-vR-sir"/>
+                                    <barButtonItem style="plain" id="8Ik-7V-jIP">
+                                        <button key="customView" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" id="9vv-Er-shl">
+                                            <rect key="frame" x="313" y="11" width="46" height="34"/>
+                                            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
+                                            <state key="normal" title="Show">
+                                                <color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
+                                            </state>
+                                            <state key="selected" title="Hide">
+                                                <color key="titleColor" cocoaTouchSystemColor="darkTextColor"/>
+                                            </state>
+                                            <connections>
+                                                <action selector="searchTableExpandableBtnClick:" destination="C3t-ED-9dx" eventType="touchUpInside" id="xY2-Xs-mHl"/>
+                                            </connections>
+                                        </button>
+                                    </barButtonItem>
+                                </items>
+                            </toolbar>
                         </subviews>
                         <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
+                        <constraints>
+                            <constraint firstAttribute="trailing" secondItem="FYC-YO-04g" secondAttribute="trailing" id="0eR-do-34B"/>
+                            <constraint firstItem="FYC-YO-04g" firstAttribute="top" secondItem="ppj-4o-rzu" secondAttribute="bottom" id="1YZ-fZ-RGu"/>
+                            <constraint firstAttribute="trailing" secondItem="s08-SO-Hkg" secondAttribute="trailing" id="5nc-ia-p71"/>
+                            <constraint firstItem="ppj-4o-rzu" firstAttribute="leading" secondItem="obf-ZQ-pbd" secondAttribute="leading" id="CUo-bm-fTo"/>
+                            <constraint firstItem="FpR-kZ-qvh" firstAttribute="top" secondItem="FYC-YO-04g" secondAttribute="bottom" id="RW1-GU-vAO"/>
+                            <constraint firstItem="ppj-4o-rzu" firstAttribute="top" secondItem="s08-SO-Hkg" secondAttribute="bottom" id="a7L-Bk-PWx"/>
+                            <constraint firstItem="s08-SO-Hkg" firstAttribute="leading" secondItem="obf-ZQ-pbd" secondAttribute="leading" id="g1w-Dn-1Dc"/>
+                            <constraint firstAttribute="trailing" secondItem="ppj-4o-rzu" secondAttribute="trailing" id="lfv-U9-za0"/>
+                            <constraint firstItem="s08-SO-Hkg" firstAttribute="top" secondItem="dkt-BH-cFp" secondAttribute="bottom" id="oN6-7x-d04"/>
+                            <constraint firstItem="FYC-YO-04g" firstAttribute="leading" secondItem="obf-ZQ-pbd" secondAttribute="leading" id="xuP-pN-h3X"/>
+                        </constraints>
                     </view>
                     <navigationItem key="navigationItem" title="Search" id="OwC-ot-lOg">
                         <barButtonItem key="rightBarButtonItem" image="ic_setting" id="cmN-H2-3ne">
@@ -1009,7 +1050,9 @@
                         </barButtonItem>
                     </navigationItem>
                     <connections>
+                        <outlet property="searchaBar" destination="NyC-qf-cxA" id="wOE-VA-NbS"/>
                         <outlet property="table" destination="ppj-4o-rzu" id="Kjn-ln-5de"/>
+                        <outlet property="tableBottomConstraint" destination="1YZ-fZ-RGu" id="Smh-1r-hxm"/>
                     </connections>
                 </viewController>
                 <placeholder placeholderIdentifier="IBFirstResponder" id="erf-Re-vM1" userLabel="First Responder" sceneMemberID="firstResponder"/>

+ 74 - 16
Apex Mobile/Apex Mobile/SearchViewController.m

@@ -16,8 +16,13 @@
 #import "MyAutocompleteView.h"
 #import "AMResultViewController.h"
 
-@interface SearchViewController ()
+@interface SearchViewController () <UISearchBarDelegate>
 @property (strong,nonatomic) SearchTableAdapter* adapter ;
+@property (strong, nonatomic) IBOutlet UISearchBar *searchaBar;
+@property (strong, nonatomic) IBOutlet NSLayoutConstraint *tableBottomConstraint;
+@property (nonatomic,assign) BOOL showTable;
+@property (nonatomic,strong) NSIndexPath *editingIndexPath;
+
 @end
 
 @implementation SearchViewController
@@ -55,8 +60,53 @@
     
       DebugLog(@"viewDidLoad");
     
+    self.table.tableFooterView = [UIView new];
+    self.searchaBar.delegate = self;
+}
+
+- (void)viewWillDisappear:(BOOL)animated {
+    [super viewWillDisappear:animated];
     
+    [[NSNotificationCenter defaultCenter] removeObserver:self];
+}
+
+- (void)keyboardWillChangeFrame:(NSNotification *)notification {
+    
+//    NSTimeInterval duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
+//    CGRect begin = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
+    CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
+    
+    CGFloat endY = CGRectGetMinY(end);
+    CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
+    CGFloat offset = screenHeight - endY;
+    CGFloat tableHeightoffset = offset - 44;
+    if (tableHeightoffset > 0) {
+        self.tableBottomConstraint.constant = offset;
+        if (self.editingIndexPath) {
+            [self.table scrollToRowAtIndexPath:self.editingIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
+        }
+    } else {
+        self.tableBottomConstraint.constant = offset;
+    }
+    [self.view layoutIfNeeded];
+}
+
+- (IBAction)searchTableExpandableBtnClick:(UIButton *)sender {
+    sender.selected = !sender.selected;
+    self.showTable = !self.showTable;
+    if (self.showTable) {
+        self.adapter=[[SearchTableAdapter alloc] init];
+        [self.adapter initfields:self.function_name];
+    } else {
+        self.adapter = nil;
+    }
+    [self.table reloadData];
 }
+
+- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
+    [self SearchBtnOnClick:nil];
+}
+
 - (IBAction)onBoolitemClick:(UIButton *)sender {
       DebugLog(@"onBoolitemClick");
     UITableViewCell *cell  = (UITableViewCell *)[[sender superview] superview];
@@ -180,6 +230,8 @@
     // 解决Items按钮灰色
     self.navigationItem.rightBarButtonItem.enabled = false;
     self.navigationItem.rightBarButtonItem.enabled = true;
+    
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
 }
 //-(void) viewWillDisappear:(BOOL)animated
 //{
@@ -213,8 +265,12 @@
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     
+    if (self.showTable) {
+        return [_adapter get_itemcount];
+    } else {
+        return 0;
+    }
     
-    return [_adapter get_itemcount];
 }
 
 //- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
@@ -425,7 +481,7 @@
         }
     }
     [params setValue:self.function_name forKey:@"module_name"];
-    
+    [params setValue:self.searchaBar.text forKey:@"keyword"];
     
 //    GridResultViewController *resultViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ResultViewController"];
 //    resultViewController.function_name = self.function_name;
@@ -495,18 +551,18 @@
 #pragma mark - Text field delegate
 
 - (void)textFieldDidBeginEditing:(UITextField *)textField {
-    int fieldpos = [textField convertPoint:textField.bounds.origin toView:self.view].y+textField.frame.size.height;
-    fieldpos+=150 ;//autocomplete height;
-    int screenheight =self.view.frame.size.height;
-    int ioffset = fieldpos - (screenheight - 216);//216 -(self.view.frame.size.height-fieldpos);
-    if(ioffset>0)
-    {
-        self.ioffset = ioffset;
-    CGPoint offset = self.table.contentOffset;
-    offset.y+=ioffset;
-        DebugLog(@"table content offset%d",ioffset);
-    [self.table setContentOffset:offset animated:YES];
-    }
+//    int fieldpos = [textField convertPoint:textField.bounds.origin toView:self.view].y+textField.frame.size.height;
+//    fieldpos+=150 ;//autocomplete height;
+//    int screenheight =self.view.frame.size.height;
+//    int ioffset = fieldpos - (screenheight - 216);//216 -(self.view.frame.size.height-fieldpos);
+//    if(ioffset>0)
+//    {
+//        self.ioffset = ioffset;
+//    CGPoint offset = self.table.contentOffset;
+//    offset.y+=ioffset;
+//        DebugLog(@"table content offset%d",ioffset);
+//    [self.table setContentOffset:offset animated:YES];
+//    }
     
     UITableViewCell *cell  = (UITableViewCell *)[[textField superview] superview];
     NSIndexPath *indexPath = [self.table indexPathForCell:cell];
@@ -519,6 +575,7 @@
     DebugLog(@"%s  row=%ld  type=%@",__func__,(long)[indexPath row], [field valueForKey:@"type"]);
     if(![[field valueForKey:@"type"] isEqualToString:@"time"])
     {
+        self.editingIndexPath = indexPath;
         self.textFieldtmp = textField;
     }
 }
@@ -545,7 +602,7 @@
 {
     
       DebugLog(@"textFieldDidEndEditing%@",textField.text);
-    
+    self.editingIndexPath = nil;
     UITableViewCell *cell  = (UITableViewCell *)[[textField superview] superview];
     
     NSIndexPath *indexPath = [self.table indexPathForCell:cell];
@@ -577,6 +634,7 @@
       DebugLog(@"textFieldShouldBeginEditing  row=%ld  type=%@",(long)[indexPath row], [field valueForKey:@"type"]);
     if(![[field valueForKey:@"type"] isEqualToString:@"time"])
     {
+        self.editingIndexPath = indexPath;
         self.textFieldtmp = textField;
         return YES;
     }