【问题标题】:Indexed Search similar to the Facebook/Google search engine类似于 Facebook/Google 搜索引擎的索引搜索
【发布时间】:2014-04-15 11:08:07
【问题描述】:

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!请帮帮我!

【问题讨论】:

  • 我提交了我的示例代码,它对我来说可以正常工作,你可以为自己定制代码

标签: ios objective-c uitableview grand-central-dispatch dispatch


【解决方案1】:

在您的 .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"]];
}

【讨论】:

  • 在这里我提交了我的代码,它对我来说就像搜索引擎谷歌搜索一样工作正常
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
相关资源
最近更新 更多