我有一个搜索栏,我想按序列号搜索 json http url。因此,如果我执行以下 json url:
http://www.ddproam.co.za/Central/Asset/AssetsWithSerial?Serial=S00000001
实际输出:
[{"AssetID":1,"AssetName":"Asset 1","AssetDesc":"This is a manually inserted Asset","AssetTypeID":1,"AssetTypeDesc":"This is a manually inserted Asset Type"}]
我正在尝试通过搜索栏搜索序列号 S00000001 以使用 json 调用的 AssetName 填充我的表格单元格。
我已将我的 json 放入一个数组并尝试执行,但我收到一个应用程序崩溃错误,说明:
@property (strong, nonatomic) NSArray *loadedSearches;
'-[__NSArrayI objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1d857700'
这是我的代码:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSString *searchQuery = [NSString stringWithFormat:@"http://www.ddproam.co.za/Central/Asset/AssetsWithSerial?Serial='%@'",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[@"AssetName"];
// refreshing the TableView when the block gets the response
[searchTableView reloadData];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
NSLog(@"%@", error.localizedDescription);
}];
[operation start];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//return self.loadedSearches.count;
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"];
}
if (self.loadedSearches[indexPath.row] == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TableCell Tapped" message:@"Yeah you tapped a table cell" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
NSLog(@"indexPath.row=%d loadedSearches.count=%d", indexPath.row, self.loadedSearches.count);
cell.textLabel.text = self.loadedSearches[indexPath.row][@"AssetName"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Perform segue to candy detail
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TableCell Tapped" message:@"Yeah you tapped a table cell" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
我想我在这里遇到了一个错误:
self.loadedSearches = JSON[@"AssetName"];