【问题标题】:UISearch and TableViewCellUISearch 和 TableViewCell
【发布时间】:2016-02-23 12:25:44
【问题描述】:

当我搜索然后选择只打开第一个字母的行时(例如 A.Others 字母不打开。NSLog 和断点没有帮助。我不明白是什么问题。

@synthesize propertyList,  letters, filteredNames, searchController , arrayPlace;

    - (void)viewDidLoad {
        [super viewDidLoad];

       ............
    filteredNames = [[NSMutableArray alloc]init];
    searchController = [[UISearchController alloc]init];


    self.searchController.searchResultsUpdater = self;

    NSString *path = [[NSBundle mainBundle] pathForResource:@"names" ofType:@"plist"];
    self.propertyList = [NSDictionary dictionaryWithContentsOfFile:path];
    self.letters = [[self.propertyList allKeys] sortedArrayUsingSelector:@selector(compare:)];

}


#pragma mark - Table view data source

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

    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell bg1.png"]];

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;



    if (tableView.tag == 1){

        NSString *letter = self.letters[indexPath.section];;
        NSArray *keyValues = [[self.propertyList[letter] allKeys] sortedArrayUsingSelector:@selector(compare:)];
        cell.textLabel.text = keyValues[indexPath.row];
    } else{
        cell.textLabel.text = filteredNames[indexPath.row];
    }

    return cell;

}

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

}

搜索栏失败,搜索后他确实选择了行 如果您想了解更多信息,只需通过答案告诉我,我将编辑我的问题,然后您将编辑您的答案

【问题讨论】:

  • 所有句子中都有“ana”,我猜是哪些搜索结果被过滤了?

标签: objective-c uitableview uisearchbar tableviewcell searchbar


【解决方案1】:

请再解释清楚。您使用任何字母进行搜索,它会显示只有“A”的结果。这是你想说的吗?如果是这样,则删除上面的代码并尝试以下方法:-

  1. 将搜索栏拖入视图控制器并将其委托设置为 self(您将在情节提要的委托属性中找到它的属性 到视图控制器)。
  2. 在 .h 文件中添加 UISearchBarDelegate,它将负责自动调用搜索栏的相应方法 委托设置为 self
  3. 使用以下方法检测搜索。您可以在此处过滤 NSArray 并重新加载表格。
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

}

我建议您先阅读一些有关 iOS 开发的基本教程,然后再深入了解。一切顺利,我希望它可以帮助你......

Screenshot

【讨论】:

  • 它显示了每个字母。但是当我点击除了第一个单元格时它不会打开详细视图
  • 请查看截图。添加在您的答案中
  • 我没有看到任何屏幕截图。请确保您已添加它
  • 我也在我的问题中添加了截图。
  • 好的...它运作良好,您面临的问题是什么?是关于 substring 吗?您需要“正在搜索的确切字词”?
猜你喜欢
  • 1970-01-01
  • 2016-06-07
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多