gpt4 book ai didi

ios - UISearchbar 解析 API

转载 作者:行者123 更新时间:2023-11-28 22:13:58 25 4
gpt4 key购买 nike

我有一个功能良好的搜索栏。只有一件事我想改进的是 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

最佳答案

有关延迟搜索的方法,请查看以下 SO 问题: Delay UISearchbar parsing

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

关于ios - UISearchbar 解析 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22238389/

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