Fa Hu
Fa Hu

Reputation: 15

TableViewController , SearchBar and TableViewCell

When I searching and after filtered select in displays row that opens only the first letter DetailView (for example A letter).Others letters don't open. NSLog and breakpoint not helping. I don't understand what is the problem.

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        NSString *keyTitle = cell.textLabel.text;

        NSDictionary *peopleUnderLetter = [self.propertyList objectForKey:self.letters[indexPath.section]];

        __block NSDictionary *selectedPerson = nil;

        [peopleUnderLetter enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {

            if ([key isEqualToString:keyTitle]) {

                selectedPerson = obj;

                *stop = YES;

            }

        }];

        if (selectedPerson) {

            DetailViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
            // Push the view controller.
            [self.navigationController pushViewController:vc animated:YES];

            [vc setDictionaryGeter:selectedPerson];
        }

    }

#pragma mark Search Display Delegate Methods

-(void)searchDisplayController:(UISearchController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

-(BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{

    [filteredNames removeAllObjects];
    if (searchString.length > 0) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchBar.text];

        for (NSString *letter in letters) {
            NSArray *matches = [[self.propertyList[letter] allKeys]filteredArrayUsingPredicate:predicate];

            [filteredNames addObjectsFromArray:matches];

        }

    }

    return YES;

}

after selecting every cell must open the detailviewcontroller with their data.Search bar shows every letter after filtered.But when I click cell doesn't open detail view (except first cell) .

Upvotes: 0

Views: 64

Answers (1)

naomimichiko
naomimichiko

Reputation: 693

Hard to tell without seeing the whole file but it looks like your problem may be this line. You are pulling the object out of self.propertyList using the section not the row. Which makes sense as to why it works only on the first row, which would have a row of 0 and an section of 0

`NSDictionary *peopleUnderLetter = [self.propertyList objectForKey:self.letters[indexPath.section]];`

Try changing it to :

`NSDictionary *peopleUnderLetter = [self.propertyList objectForKey:self.letters[indexPath.row]];`

Upvotes: 3

Related Questions