UISearchbar 解析 API

问题描述 投票:0回答:2

我有一个功能良好的搜索栏。我唯一想要改进的一件事就是 NSTimer。我不知道为什么,但是当我开始打字时,它会在延迟一段时间后搜索我的结果。它运行得很好。但对于每个输入的字母,我都会得到 1 个 API。这意味着如果我输入搜索栏,例如苏丹。我得到 5 个 API,而不是 1 个。有人可以帮助我吗?

TableViewControllerTOP.h

@interface TableViewControllerTOP : UITableViewController<UISearchDisplayDelegate, UISearchBarDelegate,UIScrollViewDelegate,UITableViewDataSource, UITableViewDelegate>
{
    
    NSTimer *myTimer;
}
@property (strong, nonatomic) IBOutlet UITableView *MainTable;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@property (nonatomic, strong) NSMutableArray *searchResults;

@property (nonatomic, retain) NSTimer *myTimer;
@property (nonatomic, assign) BOOL isAscending;

TableViewControllerTOP.m

@interface TableViewControllerTOP ()
@property (strong,nonatomic) NSMutableArray *itemss;

@end

@implementation TableViewControllerTOP
@synthesize myTimer;
@synthesize searchBar;
@synthesize searchController;
@synthesize searchResults;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.isAscending = YES;
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(pulltorefresh) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refreshControl;
      PFQuery *query = [PFQuery queryWithClassName:@"Countries"];
    query.cachePolicy = kPFCachePolicyIgnoreCache;
        [query addDescendingOrder:@"createdAt"];
   

 
    [query setLimit:10];
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        
        
        
        if (!error) {
            NSMutableArray *items = [NSMutableArray array];
            for (id obj in objects)
            {
                [items addObject:obj];
            }
            self.itemss = items;
            
            NSLog(@"%@", objects);
            NSLog(@"%lu", (unsigned long)[objects count]);
            [_MainTable reloadData];
        } else {
            
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

    
    
[self searchitems];
    
}

-(void)searchitems

{
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    
    self.tableView.tableHeaderView = self.searchBar;
    
    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    
    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;
    self.searchController.delegate = self;
    
    
    
    self.searchResults = [NSMutableArray array];
    
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    [self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];
    
    
    
    
}



-(void)filterResults:(NSString *)searchTerm {
    
    
    
    myTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
                                               target:self
                                             selector:@selector(queryask)
                                             userInfo:_itemss
                                              repeats:NO];
    
    
    
}


-(void)queryask{
    PFQuery *queryCapitalizedString = [PFQuery queryWithClassName:@"Countries"];
    [queryCapitalizedString whereKey:@"CountryTitle" containsString:[searchBar.text capitalizedString]];
    
    PFQuery *queryLowerCaseString = [PFQuery queryWithClassName:@"Countries"];
    [queryLowerCaseString whereKey:@"CountryTitle" containsString:[searchBar.text lowercaseString]];
    
    PFQuery *querySearchBarString = [PFQuery queryWithClassName:@"Countries"];
    [querySearchBarString whereKey:@"CountryTitle" containsString:searchBar.text];
    
    PFQuery *finalQuery = [PFQuery orQueryWithSubqueries:[NSArray arrayWithObjects:queryCapitalizedString,queryLowerCaseString, querySearchBarString,nil]];
    
    [finalQuery findObjectsInBackgroundWithTarget:self selector:@selector(callbackWithResult:error:)];
    


}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return 1;
}



- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterResults:searchString];
    return YES;
}

- (void)callbackWithResult:(NSArray *)items error:(NSError *)error
{
    if(!error) {
        [self.searchResults removeAllObjects];
        [self.searchResults addObjectsFromArray:items];
        [self.searchDisplayController.searchResultsTableView reloadData];
        
        
     
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == self.tableView) {
        
        
        return self.itemss.count;
        
    } else {
        
        return self.searchResults.count;
        
    }
}






- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    tableView.rowHeight = 70.0f;
}


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

CustomCell3 *cell = nil;
    cell = (CustomCell3 *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];




    if (!cell) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:nil options:nil];
        
        for (id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[CustomCell3 class]])
            {
                cell = (CustomCell3 *)currentObject;
                break;
            }
        }
    }
    
    if (tableView == self.tableView) {
        cell.MainTitle.text = [[self.itemss objectAtIndex:indexPath.row] objectForKey:@"CountryTitle"];
        cell.DescriptTitle.text = [[self.itemss objectAtIndex:indexPath.row] objectForKey:@"DescriptTitle"];
        [cell.FlagTitle setImageWithURL:[NSURL URLWithString:[[self.itemss objectAtIndex:indexPath.row] objectForKey:@"ImaURL"]]
                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        
        
        
        
    }
    
    
    
    
    
    
    if(tableView == self.searchDisplayController.searchResultsTableView) {
        
        PFObject *searchedUser = [self.searchResults objectAtIndex:indexPath.row];
        NSString *content = [searchedUser objectForKey:@"CountryTitle"];
        NSString *desco = [searchedUser objectForKey:@"DescriptTitle"];
        cell.DescriptTitle.text = desco;
        cell.MainTitle.text = content;
        NSString *image = [searchedUser objectForKey:@"ImaURL"];
        
        [cell.FlagTitle setImageWithURL:[NSURL URLWithString:image]
                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        
        
    }
    
    
    
    return cell;
}



@end
ios objective-c parse-platform uisearchbar uisearchdisplaycontroller
2个回答
1
投票

有关延迟搜索的方法,请看一下这个问题: 延迟UISearchbar解析

将其与洛根的建议结合起来可能是一个好主意。通常在输入最少字符之前不应执行搜索,以避免搜索范围过大(在许多情况下,对包含“s”的任何内容进行搜索没有多大意义,具体取决于您要搜索的内容)。


1
投票

也许您可以实现一个搜索栏委托来代替计时器,以在一些自定义逻辑之后(也许每 3 个字母)运行一个查询?

- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if ((searchBar.text.length % 3) == 0) {
        [self queryAsk];
    } 
}

注意:这种逻辑不是最好的,您可能想探索何时搜索的各种参数,但我认为这个概念比计时器有所改进,因为它更多地基于输入。 我认为这会给你更多的控制权。

© www.soinside.com 2019 - 2024. All rights reserved.