Просмотр исходного кода

Common editor add autocomplete function

Ray Zhang 3 лет назад
Родитель
Сommit
126976ac58

+ 1 - 1
common/customUI/commoneditor/CommonEditorAutoCompleteView.h

@@ -33,7 +33,7 @@
 @property (strong, nonatomic) NSString* lastSearchID;
 
 @property (nonatomic , copy) void (^returnValue)(NSString* str);
-
+-(void) performSearch:(NSString*) keyword;
 //@property (strong,nonatomic) nsa
 @end
 

+ 40 - 3
common/customUI/commoneditor/CommonEditorAutoCompleteView.m

@@ -7,7 +7,7 @@
 //
 
 #import "CommonEditorAutoCompleteView.h"
-
+#import "RAUtils.h"
 @implementation CommonEditorAutoCompleteView
 
 /*
@@ -17,6 +17,14 @@
     // Drawing code
 }
 */
+
+- (void)awakeFromNib {
+    [super awakeFromNib];
+    self.searchInput.delegate = nil;
+    // Initialization code
+}
+
+
 - (IBAction)onCancelClick:(id)sender {
 //    [self.active_field endEditing:true];
 //    self.hidden = true;
@@ -53,6 +61,20 @@
 
 
 
+}
+-(void) CancelSearch:(NSString*) keyword
+{
+    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchAutocomplete:) object:self.keyword];
+    self.arr_result = nil;
+    self.hidden=true;
+    [self.ResultTableView reloadData];
+}
+
+-(void) performSearch:(NSString*) keyword
+{
+    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchAutocomplete:) object:self.keyword];
+    self.keyword = keyword;
+    [self performSelector:@selector(searchAutocomplete:) withObject:self.keyword afterDelay:0.8];
 }
 
 - (void)textFieldDidEndEditing:(UITextField *)textField
@@ -74,6 +96,8 @@
 //- (BOOL)textFieldShouldReturn:(UITextField *)textField;              // called when 'return' key pressed. return NO to ignore.
 
 
+
+
 -(void) searchAutocomplete:(NSString*)keyword
 {
     
@@ -88,7 +112,12 @@
     dispatch_async(dispatch_get_global_queue(0, 0), ^{
         NSString* searchid=[[NSUUID new] UUIDString];
         weakself.lastSearchID=searchid;
+//        int s=[RAUtils getRandomNumber:1 to:3];
+//        sleep(s);
+//        NSLog(@"search id %@ sleep %d",searchid,s);
+
         NSArray* arr_ret=[weakself.dataSource sync_loadCadidate:keyword ];
+        
 //        NSArray* arr_ret=[weakself.dataSource sync_loadCadidate:keyword search_id:weakself.lastSearchID];
 //        self.arr_result =  [self.dataSource sync_loadCadidate:keyword search_id:[[NSUUID new] UUIDString]];
         dispatch_async(dispatch_get_main_queue(), ^{
@@ -97,6 +126,10 @@
                 self.arr_result = arr_ret;
                 [self.ResultTableView reloadData];
             }
+            else
+            {
+                NSLog(@"expired search");
+            }
 
             
         });
@@ -148,8 +181,12 @@
 //    if(self.returnValue)
 //        self.returnValue(self.arr_result[indexPath.row][@"value"]);
     
-    self.searchInput.text = self.arr_result[indexPath.row][@"value"];
-    [self.searchInput endEditing:true];
+//    self.searchInput.text = self.arr_result[indexPath.row][@"value"];
+//    [self.searchInput endEditing:true];
+    
+    
+    if(self.returnValue)
+        self.returnValue(self.arr_result[indexPath.row][@"value"]);
 //    self.searchInput.text = nil;
     self.arr_result = nil;
     self.hidden=true;

+ 8 - 11
common/customUI/commoneditor/CommonEditorAutoCompleteView.xib

@@ -1,11 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
-    <device id="retina4_7" orientation="portrait">
-        <adaptation id="fullscreen"/>
-    </device>
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="20037" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
+    <device id="retina4_7" orientation="portrait" appearance="light"/>
     <dependencies>
-        <deployment identifier="iOS"/>
-        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="20020"/>
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
     </dependencies>
     <objects>
@@ -15,15 +12,14 @@
             <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
             <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
             <subviews>
-                <toolbar opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="gPu-U2-Ob1">
+                <toolbar hidden="YES" opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="gPu-U2-Ob1">
                     <rect key="frame" x="0.0" y="20" width="375" height="44"/>
                     <items>
                         <barButtonItem style="plain" systemItem="flexibleSpace" id="Akz-J7-8Vx"/>
                         <barButtonItem style="plain" id="5fn-gW-pw6">
                             <textField key="customView" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" id="h2N-Y1-mnQ">
-                                <rect key="frame" x="92" y="7" width="220" height="30"/>
+                                <rect key="frame" x="93" y="5" width="220" height="34"/>
                                 <autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxY="YES"/>
-                                <nil key="textColor"/>
                                 <fontDescription key="fontDescription" type="system" pointSize="14"/>
                                 <textInputTraits key="textInputTraits"/>
                                 <connections>
@@ -40,7 +36,7 @@
                     </items>
                 </toolbar>
                 <tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="z0F-88-nmc">
-                    <rect key="frame" x="0.0" y="64" width="375" height="603"/>
+                    <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                     <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                     <connections>
                         <outlet property="dataSource" destination="iN0-l3-epB" id="XKK-5p-2we"/>
@@ -51,8 +47,8 @@
             <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
             <constraints>
                 <constraint firstItem="gPu-U2-Ob1" firstAttribute="width" secondItem="iN0-l3-epB" secondAttribute="width" id="B1l-PN-MiB"/>
+                <constraint firstItem="z0F-88-nmc" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" id="BQa-co-H1R"/>
                 <constraint firstItem="z0F-88-nmc" firstAttribute="width" secondItem="gPu-U2-Ob1" secondAttribute="width" id="E6u-nm-wlS"/>
-                <constraint firstItem="z0F-88-nmc" firstAttribute="top" secondItem="gPu-U2-Ob1" secondAttribute="bottom" id="ago-Mp-q7y"/>
                 <constraint firstItem="z0F-88-nmc" firstAttribute="centerX" secondItem="gPu-U2-Ob1" secondAttribute="centerX" id="l4D-Tt-srx"/>
                 <constraint firstItem="gPu-U2-Ob1" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="20" id="rkt-8D-rQZ"/>
                 <constraint firstItem="gPu-U2-Ob1" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="sdE-t2-I5l"/>
@@ -62,6 +58,7 @@
                 <outlet property="ResultTableView" destination="z0F-88-nmc" id="bho-eb-FvI"/>
                 <outlet property="searchInput" destination="h2N-Y1-mnQ" id="Vvu-mS-mwQ"/>
             </connections>
+            <point key="canvasLocation" x="134" y="133"/>
         </view>
     </objects>
 </document>

+ 93 - 7
common/customUI/commoneditor/CommonEditorViewController.m

@@ -4850,7 +4850,16 @@ if(!self.alert.isBeingDismissed)
     return myView;
 }
 
-
+- (void)scrollViewDidScroll:(UIScrollView *)scrollView
+{
+    NSLog(@"Did Scroll %f",scrollView.contentOffset.y);
+//    CGPoint contentOffsetPoint = scrollView.contentOffset;
+    CGRect f = self.autocompleteView.frame;
+    CGRect rframe=[RAUtils relativeFrameForScreenWithView:self.lastedit];
+    NSLog(@"edit y %f",rframe.origin.y);
+    self.autocompleteView.frame = CGRectMake(f.origin.x, rframe.origin.y+rframe.size.height, f.size.width, f.size.height);
+    
+}
 //
 -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
@@ -6034,10 +6043,35 @@ if(!self.alert.isBeingDismissed)
     
     NSString* keyboard = [item_json valueForKey:@"keyboard"];
     if(![keyboard isEqualToString:@"number"] && ![keyboard isEqualToString:@"int"])
-    {     if(lenth==0)
+    {
+        if(lenth==0)
+        {
+            if([item_json[@"autocomplete"] boolValue])
+            {
+                self.autocompleteView.hidden = false;
+               
+                    CGRect f = self.autocompleteView.frame;
+                    CGRect rframe=[RAUtils relativeFrameForScreenWithView:textField];
+                    NSLog(@"edit y %f",rframe.origin.y);
+                    self.autocompleteView.frame = CGRectMake(f.origin.x, rframe.origin.y+rframe.size.height, f.size.width, f.size.height);
+             
+                [self.autocompleteView performSearch:textField.text];
+            }
             return TRUE;
+        }
         else
         {
+            if([item_json[@"autocomplete"] boolValue])
+            {
+                self.autocompleteView.hidden = false;
+               
+                    CGRect f = self.autocompleteView.frame;
+                    CGRect rframe=[RAUtils relativeFrameForScreenWithView:textField];
+                    NSLog(@"edit y %f",rframe.origin.y);
+                    self.autocompleteView.frame = CGRectMake(f.origin.x, rframe.origin.y+rframe.size.height, f.size.width, f.size.height);
+             
+                [self.autocompleteView performSearch:textField.text];
+            }
             return (lenth>textField.text.length ||[string isEqualToString:@""]);
         }
     }
@@ -6052,13 +6086,48 @@ if(!self.alert.isBeingDismissed)
     BOOL canChange = [string isEqualToString:filtered];
     
     
+//    if([item_json[@"autocomplete"] boolValue]==true&&canChange)
+//    {
+//
+//    }
+    
     if(lenth==0)
+    {
+        if([item_json[@"autocomplete"] boolValue])
+        {
+            self.autocompleteView.hidden = false;
+           
+                CGRect f = self.autocompleteView.frame;
+                CGRect rframe=[RAUtils relativeFrameForScreenWithView:textField];
+                NSLog(@"edit y %f",rframe.origin.y);
+                self.autocompleteView.frame = CGRectMake(f.origin.x, rframe.origin.y+rframe.size.height, f.size.width, f.size.height);
+         
+            [self.autocompleteView performSearch:textField.text];
+            
+        }
         return canChange;
+    }
     else
     {
+        if([item_json[@"autocomplete"] boolValue])
+        {
+            self.autocompleteView.hidden = false;
+           
+                CGRect f = self.autocompleteView.frame;
+                CGRect rframe=[RAUtils relativeFrameForScreenWithView:textField];
+                NSLog(@"edit y %f",rframe.origin.y);
+                self.autocompleteView.frame = CGRectMake(f.origin.x, rframe.origin.y+rframe.size.height, f.size.width, f.size.height);
+         
+            [self.autocompleteView performSearch:textField.text];
+        }
         return canChange&& (lenth>textField.text.length ||[string isEqualToString:@""]);
     }
 }
+
+//- (void)textFieldDidChangeSelection:(UITextField *)textField
+//{
+//
+//}
 - (void)textFieldDidEndEditing:(UITextField *)textField
 {
     
@@ -6074,7 +6143,10 @@ if(!self.alert.isBeingDismissed)
             textField.text = fillText;
         }
     }
-    
+    if([item_json[@"autocomplete"] boolValue])
+    {
+        self.autocompleteView.hidden = true;
+    }
     [self textfieldSetValue:indexPath value:textField.text];
     //    NSMutableDictionary* section_json=nil;
     //
@@ -6356,7 +6428,12 @@ if(!self.alert.isBeingDismissed)
         }
     }
 }
-
+- (BOOL)textFieldShouldReturn:(UITextField *)textField
+{
+//    [self searchAction];
+    [textField resignFirstResponder];
+    return YES;
+}
 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
     
     self.editingcell = (UITableViewCell*)textField.superview.superview;
@@ -6367,9 +6444,10 @@ if(!self.alert.isBeingDismissed)
     if([item_json[@"autocomplete"] boolValue]==true)
     {
 //        [textField endEditing:true];
+        
             self.lastedit_from  = [self.editorTable indexPathForCell:(UITableViewCell*) textField.superview.superview];
         self.autocompleteView.active_field = textField;
-        self.autocompleteView.hidden = false;
+//        self.autocompleteView.hidden = false;
         self.autocompleteView.searchInput.keyboardType=textField.keyboardType;
         self.autocompleteView.searchInput.text= textField.text;
         [self.autocompleteView.searchInput becomeFirstResponder];
@@ -6382,7 +6460,12 @@ if(!self.alert.isBeingDismissed)
             
             [weakself textFieldDidEndEditing:textField];
         };
-        return false;
+        
+        CGRect rframe=[RAUtils relativeFrameForScreenWithView:textField];
+        
+        self.autocompleteView.frame= CGRectMake(rframe.origin.x,rframe.origin.y+rframe.size.height,rframe.size.width,400);//CGRectMake(screenframe.origin.x, screenframe.origin.y, screenframe.size.width, end_y);
+        
+//        return false;
     }
     
     return YES;
@@ -6806,7 +6889,8 @@ if(!self.alert.isBeingDismissed)
     CGFloat end_y = end.origin.y;
     
     CGRect screenframe=[[UIScreen mainScreen] bounds];
-    self.autocompleteView.frame= CGRectMake(screenframe.origin.x, screenframe.origin.y, screenframe.size.width, end_y);
+//    CGRect controlframe= self.lastedit.frame;
+//    self.autocompleteView.frame= CGRectMake(200,200,200,200);//CGRectMake(screenframe.origin.x, screenframe.origin.y, screenframe.size.width, end_y);
     
     NSIndexPath *indexPath = [self.editorTable indexPathForCell:self.editingcell];
     
@@ -6854,6 +6938,7 @@ if(!self.alert.isBeingDismissed)
                 
                 
             }
+
         } else {
             // contentInset = (top = 64, left = 0, bottom = 0, right = 0),topAvailableHeight = -64
             table_frame.size.height = table_origin_screen_frame.size.height - offset;
@@ -6883,6 +6968,7 @@ if(!self.alert.isBeingDismissed)
         }
     }
 
+    
     self.editorTable.frame = table_frame;
     
     CGRect cell_screen_frame = [self.editingcell convertRect:self.editingcell.bounds toView:self.view.window];