TableViewController,SearchBar和TableViewCell

时间:2016-02-24 20:23:07

标签: objective-c xcode uitableview uisearchbar cell-array

当我搜索并在过滤后选择显示行仅打开第一个字母DetailView(例如A字母)。其他字母不打开。 NSLog和断点没有帮助。我不明白是什么问题。

     - (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;

}

选择每个单元格后必须打开带有数据的detailviewcontroller。搜索栏显示过滤后的每个字母。但是当我单击单元格时,不会打开详细视图(第一个单元格除外)。

1 个答案:

答案 0 :(得分:3)

很难说没有看到整个文件,但看起来你的问题可能就是这一行。您使用部分而不是将对象从self.propertyList中拉出。这对于为什么它仅适用于第一行有意义,第一行的行为0,部分为0

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

尝试将其更改为:

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