例:
用户在UISearchbar中键入“union”,但UItableVIEw中没有显示任何结果.用户在“联合”,“联合”之后键入任何字符,并且“联合”的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
SearchVIEwController.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,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,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
@H_301_31@解决方法 问题似乎是,在使用AFJsONRequestoperation进行异步 *** 作时,在获取数据之前刷新表.所以你的模型可能正在更新,但你的tablevIEw是一个刷新背后.尝试在块成功回调中移动[searchtableVIEw reloadData]:AFJsONRequestoperation *operation = [AFJsONRequestoperation JsONRequestoperationWithRequest:request success:^(NSURLRequest *request,ID JsON) { self.loadedSearches = JsON[@"response"][@"venues"]; // refreshing the tableVIEw when the block gets the response [searchtableVIEw reloadData]; } failure:^(NSURLRequest *request,ID JsON) { NSLog(@"%@",error.localizedDescription); }];
希望这有效.
总结以上是内存溢出为你收集整理的xcode – 使用UISearchBar调用API全部内容,希望文章能够帮你解决xcode – 使用UISearchBar调用API所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)