gpt4 book ai didi

ios - 类似于 Facebook/Google 搜索引擎的索引搜索

转载 作者:行者123 更新时间:2023-11-29 03:03:09 25 4
gpt4 key购买 nike

在 UIViewController 中进行索引搜索!在插入文本字段研究中的任何字母时,都会调用一个方法来执行数据库查询以获取作为字符串函数的元组。如果我一个接一个地写信,应用程序就会崩溃,因为该方法仍在运行时被再次调用。相反,如果我写一封信,期待调度队列完成并再写一封信,一切正常!但我想按照第一种情况所述执行!这里的代码:

- (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/google 搜索引擎的方式进行研究?鉴于我不使用 UISearchDisplayController,但我只使用一个文本字段,它在每个 [Editing Did Change] 调用方法 searchUser 并且它用所有结果填充 Tableview!请帮助我!

最佳答案

在你的 .h 文件中声明数组名

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


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

@property (strong, nonatomic) UITableView *tablevie;

在您的 .m 文件中

@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)
{

textField.text=@"";

}

// 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"]];
}

关于ios - 类似于 Facebook/Google 搜索引擎的索引搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23081915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com