Как использовать NSObject в UISearchResultsUpdating в Objective-C - PullRequest
0 голосов
/ 16 октября 2019

Я получил 2 массива, в одном из которых я использую поиск. Первое отображение массива в cell.textLabel Второе отображение в cell.detailedTextLabel. После поиска первый массив отображается в виде таблицы правильно, второй массив отображается без изменений

Я знаю, что мне нужно использовать NSObject, но я не знаю, как. Я не могу понять, как вставить все значения из двух разных NSArray в NSObject.

NSObject:

- (instancetype) initWithDictionary: (NSDictionary*) dictionary {
self = [super init];    
if (self) {
    self.city = [dictionary valueForKey:@"city"]; 
    self.IATA=  [dictionary valueForKey:@"IATA"];;
}
return self;
}

Поиск:

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    if (searchController.searchBar.text) {

        NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchController.searchBar.text]; 
        self.searchResultCity  = [self.cityNames1 filteredArrayUsingPredicate:predicate];


        if ([self.temp objectForKey:searchController.searchBar.text]) {
            NSLog(@"проверка словаря");
        }
        NSLog(@"%@", self.searchResultCity);
        [self.tableView reloadData];
    }
}

UITableViewCell:

- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }

    if (self.search.isActive && [self.searchResultCity count] > 0) {

        [cell.textLabel setText:[self.searchResultCity objectAtIndex:indexPath.row]] ;
        FlyRateModel* new = [[FlyRateModel alloc] initWithDictionary:self.temp];
        NSLog(@"FlyRateModel =%@", new.city);

        [cell.detailTextLabel setText:[self.searchResultIATA objectAtIndex:indexPath.row]];

    } else {
            [cell.textLabel setText:[self.cityNames1 objectAtIndex:indexPath.row]] ;
            [cell.detailTextLabel setText:[self.IATA objectAtIndex:indexPath.row]];
       }
    return cell;
}

NSDictionary "temp" получает 2 массива:

            self.temp = @{self.cityNames1: self.IATA};
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...