IOS 8 Objective-C的搜索栏和表视图使用谷歌自动完成 [英] IOS 8 Objective-C Search bar and Table view Using Google Autocomplete
问题描述
我是新来的iOS开发和我一直在坚持这个问题了两个星期。我试图与谷歌从搜索结果中自动完成功能的搜索栏。最近我已经能够按照此教程,使这是通过混合伪造。 (我发现其他教程不使用ARC的,并且是pretty已经多日。)
我能看到URL连接是通过NSLog的正常工作,但是当我重新分配我的suggestionArray的问题所在。如果我注释掉这一部分:
_suggestionArray = [NSArray的arrayWithArray:JSON [@responseData] [@成果]];
[_tableView reloadData];
它给这个错误:
[__ NSCFDictionary isEqualToString:]:无法识别的选择发送到实例0x7f968be3a060
2015年5月21日17:43:11.277自动完成[865:65704] ***终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:' - [__ NSCFDictionary isEqualToString:]:无法识别的选择发送到实例0x7f968be3a060
***第一掷调用堆栈:
(
0的CoreFoundation 0x00000001077daa75 __exception preprocess + 165
1 libobjc.A.dylib 0x0000000107473bb7 objc_exception_throw + 45
2的CoreFoundation 0x00000001077e1d1d - [NSObject的(NSObject的)doesNotRecognizeSelector:] + 205
3的CoreFoundation 0x00000001077399dc ___forwarding___ + 988
4的CoreFoundation 0x0000000107739578 _CF_forwarding_ prep_0 + 120
5 UIKit的0x0000000107f4cdbd - [UITableViewLabel的setText:] + 81
6自动完成0x0000000106f40acd - [ViewController中的tableView:的cellForRowAtIndexPath:] + 333
7 UIKit的0x0000000107cc1e03 - [UITableView的_create preparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
8 UIKit的0x0000000107ca1901 - [UITableView的_updateVisibleCellsNow:isRecursive:] + 2846
9 UIKit的0x0000000107cb778c - [UITableView的layoutSubviews] + 213
10 UIKit的0x0000000107c441c3 - [UIView的(CALayerDelegate)layoutSublayersOfLayer:] + 521
11 QuartzCore 0x000000010b4d6c58 - [CALayer的layoutSublayers] + 150
12 QuartzCore 0x000000010b4cb87e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
13 QuartzCore 0x000000010b4cb6ee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
14 QuartzCore 0x000000010b43936e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
15 QuartzCore 0x000000010b43a482 _ZN2CA11Transaction6commitEv + 390
16 QuartzCore 0x000000010b43aaed _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
17的CoreFoundation 0x000000010770f507 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18的CoreFoundation 0x000000010770f460 __CFRunLoopDoObservers + 368
19的CoreFoundation 0x0000000107705293 __CFRunLoopRun + 1123
20的CoreFoundation 0x0000000107704bc6 CFRunLoopRunSpecific + 470
21 GraphicsServices 0x000000010adcaa58 GSEventRunModal + 161
22 UIKit的0x0000000107bca580 UIApplicationMain + 1282
23自动完成0x0000000106f41663主+ 115
24 libdyld.dylib 0x0000000109d88145启动+ 1
)
的libc ++ abi.dylib:类型NSException的未捕获的异常终止
(LLDB)
下面是我的code以下:
[ViewController.h]
#进口<的UIKit / UIKit.h>@interface的ViewController:UIViewController中
@property(弱,非原子)IBOutlet中的UITableView *的tableView;
@property(弱,非原子)IBOutlet中的UISearchBar *搜索栏;
@property(强,读写,非原子)的NSArray * suggestionArray;@结束
[ViewController.m]
#进口ViewController.h@interface的ViewController()@结束@implementation的ViewController - (无效)viewDidLoad中{
[超级viewDidLoad中];
//装载视图,通常从笔尖后做任何额外的设置。
} - (空)didReceiveMemoryWarning {
[超级didReceiveMemoryWarning];
可以重新创建任何资源的处置//。
}的#pragma马克 - UITableViewDataSource方法 - (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分{
返回_suggestionArray.count;
} - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath {
静态的NSString * CellIdentifier = @细胞; * UITableViewCell的细胞= [的tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 如果(细胞==零){
电池= [[UITableViewCell的页头] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} cell.textLabel.text = [_suggestionArray objectAtIndex:indexPath.row]; 返回细胞;
}的#pragma马克 - 的UITableViewDelegate方法 - (无效)的tableView:(NSIndexPath *)indexPath {
_searchBar.text = [_suggestionArray objectAtIndex:indexPath.row];
} - (无效)搜索栏:(*的UISearchBar)搜索栏textDidChange:(* NSString的){SEARCHTEXT
[SEARCHTEXT stringByReplacingOccurrencesOfString:@withString:@+];
[SEARCHTEXT stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 的NSLog(@%@,SEARCHTEXT); NSURLSession *会话= [NSURLSession sharedSession]
NSURLSessionDataTask * dataTask = [会议dataTaskWithURL:[NSURL URLWithString:[的NSString stringWithFormat:@\"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&q=%@\",searchText]] completionHandler:^(NSData的*数据,NSURLResponse *响应,NSError *错误){
*的NSDictionary JSON = [NSJSONSerialization JSONObjectWithData:数据选项:0错误:无];
的NSLog(@连接测试标记);
的NSLog(@%@,[NSArray的arrayWithArray:JSON [@responseData] [@成果]]);
// _ suggestionArray = [NSArray的arrayWithArray:[NSArray的arrayWithArray:JSON [@responseData] [@成果]]];
// [_的tableView reloadData];
}]; [dataTask简历]
}@结束
我也没有从搜索显示控制器表视图按Ctrl +拖拽选择searchResultsDelegate(把它在这里,以防万一它事项)。
任何帮助将AP preciated。谢谢! :)
NSURLSessionDataTask * dataTask = [会议dataTaskWithURL:[NSURL URLWithString:[的NSString stringWithFormat:@\"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&q=%@\",searchText]] completionHandler:^(NSData的*数据,NSURLResponse *响应,NSError *错误){
*的NSDictionary JSON = [NSJSONSerialization JSONObjectWithData:数据选项:0错误:无];
如果(!JSON){//是一个有效的JSON?
返回;
}
*的NSDictionary = jsonDict [JSON objectForKey:@responseData];
_suggestionArray = = [jsonDict objectForKey:@结果] [_tableView reloadData];
}];
然后改变键(例如:titleNoFormatting)
- (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath {
静态的NSString * CellIdentifier = @细胞;* UITableViewCell的细胞= [的tableView dequeueReusableCellWithIdentifier:CellIdentifier];如果(细胞==零){
电池= [[UITableViewCell的页头] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}cell.textLabel.text = [[_suggestionArray objectAtIndex:indexPath.row] valueForKey:@titleNoFormatting];返回细胞;
}
I'm new to iOS development and I've been stuck with this problem for two weeks now. I'm trying to make a search bar with an autocomplete function from google search results. The closest I've been able to make this is by following this tutorial by Hybrid Forge. (Other tutorials I found don't make use of ARC, and are pretty much dated already.)
I am able to see that the URL connection is working properly through NSLog, but the problem lies when I reassign my suggestionArray. If I comment out this part:
_suggestionArray = [NSArray arrayWithArray:json[@"responseData"][@"results"]];
[_tableView reloadData];
it gives this error:
[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x7f968be3a060
2015-05-21 17:43:11.277 AutoComplete[865:65704] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x7f968be3a060'
*** First throw call stack:
(
0 CoreFoundation 0x00000001077daa75 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000107473bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001077e1d1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001077399dc ___forwarding___ + 988
4 CoreFoundation 0x0000000107739578 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000107f4cdbd -[UITableViewLabel setText:] + 81
6 AutoComplete 0x0000000106f40acd -[ViewController tableView:cellForRowAtIndexPath:] + 333
7 UIKit 0x0000000107cc1e03 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
8 UIKit 0x0000000107ca1901 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
9 UIKit 0x0000000107cb778c -[UITableView layoutSubviews] + 213
10 UIKit 0x0000000107c441c3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
11 QuartzCore 0x000000010b4d6c58 -[CALayer layoutSublayers] + 150
12 QuartzCore 0x000000010b4cb87e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
13 QuartzCore 0x000000010b4cb6ee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
14 QuartzCore 0x000000010b43936e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
15 QuartzCore 0x000000010b43a482 _ZN2CA11Transaction6commitEv + 390
16 QuartzCore 0x000000010b43aaed _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
17 CoreFoundation 0x000000010770f507 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18 CoreFoundation 0x000000010770f460 __CFRunLoopDoObservers + 368
19 CoreFoundation 0x0000000107705293 __CFRunLoopRun + 1123
20 CoreFoundation 0x0000000107704bc6 CFRunLoopRunSpecific + 470
21 GraphicsServices 0x000000010adcaa58 GSEventRunModal + 161
22 UIKit 0x0000000107bca580 UIApplicationMain + 1282
23 AutoComplete 0x0000000106f41663 main + 115
24 libdyld.dylib 0x0000000109d88145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Here's my code for the following: [ViewController.h]
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, readwrite, nonatomic) NSArray *suggestionArray;
@end
[ViewController.m]
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
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];
return cell;
}
#pragma mark - UITableViewDelegate Methods
- (void)tableView:(NSIndexPath *)indexPath {
_searchBar.text = [_suggestionArray objectAtIndex:indexPath.row];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[searchText stringByReplacingOccurrencesOfString:@" " withString:@"+"];
[searchText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", searchText);
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&q=%@",searchText]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"connection test marker");
NSLog(@"%@", [NSArray arrayWithArray:json[@"responseData"][@"results"]]);
//_suggestionArray = [NSArray arrayWithArray:[NSArray arrayWithArray:json[@"responseData"][@"results"]]];
//[_tableView reloadData];
}];
[dataTask resume];
}
@end
I also did the Ctrl+drag from the search display controller to the table view and selected the "searchResultsDelegate" (putting it out here just in case it matters).
Any help will be appreciated. Thank you! :)
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&q=%@",searchText]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if(!json){ // is a valid json ?
return;
}
NSDictionary * jsonDict = [json objectForKey:@"responseData"];
_suggestionArray = = [jsonDict objectForKey:@"results"]
[_tableView reloadData];
}];
Then change the key ( for example :"titleNoFormatting")
- (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] valueForKey:@"titleNoFormatting"];
return cell;
}
这篇关于IOS 8 Objective-C的搜索栏和表视图使用谷歌自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!