2014-04-15 133 views
0

索引搜索UIViewController!在任何插入到textfield研究中的字母中,都會調用一個方法來執行數據庫查詢以獲取作爲字符串函數的元組。 如果我在字母后面寫了一個字母,則應用程序會崩潰,因爲在該方法仍在運行時會再次調用該方法。相反,如果我寫了一封信,期待完成調度隊列並寫下另一封信,一切正常!但我想按照第一種情況的描述進行表演! 下面的代碼:已收錄搜索類似於Facebook的/谷歌搜索引擎

- (IBAction)searchUser:(id)sender{ 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [_arrID removeAllObjects]; 
     [_arrNames removeAllObjects]; 
     [_arrPictures removeAllObjects]; 
     [self.tableView reloadData]; 
    }); 

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       CGRect activityFrame = CGRectMake(self.view.center.x, self.view.center.y, 0.0, 0.0); 

       _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:activityFrame]; 

       [[self.view superview] addSubview:_activityIndicator]; 

       _activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; 
       _activityIndicator.color = [UIColor whiteColor]; 
       [_activityIndicator startAnimating]; 
      }); 

//Here I fill the 3 arrays with details obtained from external queries on database 

      dispatch_async(dispatch_get_main_queue(), ^{ 

       [self.tableView reloadData]; 

       [_activityIndicator stopAnimating]; 
      }); 
     }); 
    } 
} 

有沒有一種解決方案,類似facebook的/谷歌搜索引擎的方式來進行研究?鑑於我沒有使用UISearchDisplayController,但我只是使用一個文本字段,每次[Editing Did Change]都會調用searchUser方法,並將所有結果填充到Tableview中!請幫幫我!

在.h文件中
+0

提交我的示例代碼將工作的罰款對我來說,U可以自定義代碼你的自我 –

回答

1

在.m文件聲明數組名

@interface newactivecomposeViewController : UIViewController< 
UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate> 
{ 
BOOL SelectionAvailable; 
} 


@property (strong, nonatomic) NSMutableArray *AllDetails,*SuggestionArray; 

@property (strong, nonatomic) UITableView *tablevie; 

@synthesize SuggestionArray,AllDetails,tablevie; 

- (void)viewDidLoad 
{ 
SuggestionArray=[[NSMutableArray alloc]init]; //for using the searching 
self.AllDetails=[[NSMutableArray alloc] init]; //store the all name in this array 
tablevie=[[UITableView alloc]initWithFrame:CGRectMake(23, 136, 277, 214)]; //tableview created dynamically 
tablevie.dataSource=self; 
tablevie.delegate=self; 

[self.tablevie registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; 


} 



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 


     [self.view addSubview:tablevie]; 


    NSString *substring = [NSString stringWithString:textField.text]; 
    substring = [substring stringByReplacingCharactersInRange:range withString:string]; 
    [self searchAutocompleteEntriesWithSubstring:substring]; 
    return YES; 


} 


- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring { 
    [SuggestionArray removeAllObjects]; 
    for (NSDictionary *tmp in self.AllDetails) { 
    NSRange substringRange =[[[tmp objectForKey:@"name"] lowercaseString] rangeOfString:[substring lowercaseString]]; 
    if (substringRange.location==0) { 
     [SuggestionArray addObject:tmp]; 
    } 
} 

if ([SuggestionArray count]==0) { 
    tablevie.hidden=YES; 

    } 
else { 

    tablevie.hidden=NO; 
} 
[tablevie reloadData]; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 

{ 


    for (NSString *tmp in getfrinendname) { 
     NSLog(@"%@", tmp); 
     if ([[textField.text lowercaseString] isEqualToString:[tmp lowercaseString]])  { 
      textField.text=tmp; 

      SelectionAvailable=YES; 
      break; 
     } 
     else { 
      SelectionAvailable=NO; 
     } 
    } 

    if (!SelectionAvailable) { 

     if (textField.text.length==0) 
     { 

      [email protected]""; 

     } 

//   else if (textField.text.length >0) 
//   { 
//     
//    [textField resignFirstResponder]; 
//    [tablevie setHidden:YES]; 
//     
//   } 
     else 
     { 
      Alert=[[UIAlertView alloc]initWithTitle:@"User not found!" message:@"Please try again" delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
      [Alert show]; 
     } 



    } 



#pragma tablevie data source 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
NSLog(@"the count==%lu",(unsigned long)[SuggestionArray count]); 
return [SuggestionArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; 

    } 


cell.textLabel.text=[[SuggestionArray objectAtIndex:indexPath.row] objectForKey:@"name"]; //load your key or index 
return cell; 

} 


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
yourtextfieldname.text=[NSString stringWithFormat:@"%@",[[SuggestionArray objectAtIndex:indexPath.row] objectForKey:@"name"]]; 
} 
+1

在這裏我提交我的代碼,它的工作很好,就像搜索引擎谷歌搜索 –