我有一个 UITableView 显示来自 API 的结果。每当用户通过 searchBar:textDidChange: 输入 UISearchBar 时,都会调用该 API。有效地实现自动完成搜索。我的问题是加载到 UITableView 中的结果似乎是最后一次 API 调用之后的迭代。
示例:用户在 UISearchBar 中键入“union”,但 UITableView 中未显示任何结果。用户在“union”之后键入任何字符,例如“unions”,“union”的API结果显示在UITableView中。当用户向下滚动结果(“联合”,但实际上是“联合”)时,“重新填充的单元格”显示“联合”结果。
SearchViewController.h
#import <UIKit/UIKit.h>
@interface SearchViewController : UIViewController <UITextFieldDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate>{
UITableView *searchTableView;
UISearchBar *sBar;
UISearchDisplayController *searchDisplayController;
}
@property (strong, nonatomic) NSArray *loadedSearches;
@end
搜索视图控制器.m
#import "SearchViewController.h"
#import "AFJSONRequestOperation.h"
@interface SearchViewController ()
@end
@implementation SearchViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = @"Search";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
searchTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
searchTableView.delegate = self;
searchTableView.dataSource = self;
[self.view addSubview:searchTableView];
sBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 160, 44)];
sBar.placeholder = @"Bus Route to...";
sBar.delegate = self;
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:sBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = searchTableView.dataSource;
searchDisplayController.searchResultsDelegate = searchTableView.delegate;
searchTableView.tableHeaderView = sBar;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSString *searchQuery = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=40.4263,-86.9177&client_id=xxx&client_secret=yyy&v=20121223&query='%@'",searchText];
searchQuery = [searchQuery stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:searchQuery];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
self.loadedSearches = JSON[@"response"][@"venues"];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
NSLog(@"%@", error.localizedDescription);
}];
[operation start];
[searchTableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.loadedSearches.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.textLabel.text = self.loadedSearches[indexPath.row][@"name"];
return cell;
}
@end
如果我的问题不清楚,请告诉我。
随意批评代码的其他方面,但我真的很感激我的问题的解决方案:) 提前致谢。
示例 API 响应 - http://pastebin.com/UZ1H2Zwy