gpt4 book ai didi

ios - 如何显示单词的结果?

转载 作者:行者123 更新时间:2023-12-01 20:20:08 25 4
gpt4 key购买 nike

我在iOS中制作字典,从JSON网址获取数据。我从SearchBar正确显示单词的结果时遇到问题。例如,当我在searchBar中键入某些内容并单击某个结果时,总是显示第一个结果的值。

下面,我将屏幕放在屏幕上以显示此问题。

enter image description here



enter image description here

在第二个屏幕上,我单击Ruby on Rails并向我显示结果“Objective C”的第一个,为什么(Objective C是JSON文件中的第一个结果)?
这是我的代码:http://pastebin.com/EPVTpF9U

    #pragma mark - Navigation
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"pushDetailView"]){
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *modalVC = (DetailViewController*)segue.destinationViewController;
modalVC.detailArray = [self.finalResultArray objectAtIndex:indexPath.row];
}
}

#pragma mark - UISearchDisplayDelegate
// register a cell reuse identifier for the search results table view
-(void)searchDisplayController:(UISearchDisplayController *)controller
didLoadSearchResultsTableView:(UITableView *)tableView {
[tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:@"Cell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(tableView == self.tableView){
return self.finalResultArray.count;
}else{
return self.results.count;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdetifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdetifier forIndexPath:indexPath];

if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdetifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(tableView == self.tableView){
cell.textLabel.text = [[self.finalResultArray objectAtIndex:indexPath.row] objectForKey:@"expression"];
cell.detailTextLabel.text = [[self.finalResultArray objectAtIndex:indexPath.row] objectForKey:@"meaning"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else{
cell.textLabel.text = [[self.results objectAtIndex:indexPath.row] objectForKey:@"expression"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor colorWithRed:180/255.0
green:138/255.0
blue:171/255.0
alpha:0.5];
cell.selectedBackgroundView = customColorView;
return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"pushDetailView" sender:self];
}

- (void)simpleJsonParsing
{
//-- Make URL request with server
NSHTTPURLResponse *response = nil;
NSString *jsonUrlString = [NSString stringWithFormat:@"https://uidictionary.herokuapp.com/phrases.json"];
NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

//-- Get request and response though URL
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

//-- JSON Parsing
NSArray *result = [[NSJSONSerialization JSONObjectWithData:responseData options:
NSJSONReadingMutableContainers error:nil] objectForKey:@"phrases"];

[self.finalResultArray removeAllObjects];
for (NSMutableDictionary *tmp in result)
{
NSMutableDictionary *temp = [NSMutableDictionary new];
[temp setObject:[tmp objectForKey:@"expression"] forKey:@"expression"];
[temp setObject:[tmp objectForKey:@"meaning"] forKey:@"meaning"];

[self.finalResultArray addObject:temp];
}
if (self.finalResultArray){
[self.tableView reloadData];
}
}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
self.results = [self.finalResultArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K contains[c] %@", @"expression", searchController.searchBar.text]];
NSLog(@"Filterd Array:-%@", self.results);

// hand over the filtered results to our search results table
UITableViewController *tableController = (UITableViewController *)self.searchController.searchResultsController;
tableController.tableView.dataSource = self;
tableController.tableView.delegate = self;
[tableController.tableView reloadData];
}

最佳答案

只需替换波纹管代码:-

self.results = [self.finalResultArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K BEGINSWITH[c] %@", @"expression", searchController.searchBar.text]];

关于ios - 如何显示单词的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35841271/

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