• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UISearchBar 在搜索结果中显示多个部分标题

[复制链接]
菜鸟教程小白 发表于 2022-12-13 08:54:48 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个应用程序,当有人使用搜索栏过滤 UITableView 中的对象时,会显示两个部分标题。一个是静止的,另一个随着搜索结果移动。请参阅附上的两个屏幕截图。

下面我已经包含了我的应用程序中的 tableview 方法,任何帮助或建议将不胜感激。

enter image description here

enter image description here

        - (void) performFetch
    {

        [NSFetchedResultsController deleteCacheWithName"Master"];  

        // Init a fetch request
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName"MainObject" inManagedObjectContext:self.managedObjectContext];
        [fetchRequest setEntity:entity];

        // Apply an ascending sort for the color items
        //NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey"Term" ascending:YES selector:nil];
        NSSortDescriptor *sortDescriptor;
        if(sortValue==1)
        {
            sortDescriptor = [[NSSortDescriptor alloc] initWithKey"fullName" ascending:YES selectorselector(caseInsensitiveCompare];
        }
        else if(sortValue==2)
        {
            sortDescriptor = [[NSSortDescriptor alloc] initWithKey"firstName" ascending:YES selectorselector(caseInsensitiveCompare];
        }
        else if(sortValue==3)
        {
            sortDescriptor = [[NSSortDescriptor alloc] initWithKey"socialSecurity" ascending:YES selectorselector(caseInsensitiveCompare];
        }
        NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
        [fetchRequest setSortDescriptors:descriptors];

        // Recover query
        NSString *query = self.searchDisplayController.searchBar.text;        
        if (query && query.length) fetchRequest.predicate = [NSPredicate predicateWithFormat"(fullName contains[cd] %@) || (socialSecurity contains[cd] %@)",query, query];

        // Init the fetched results controller
        NSError *error;
        if(sortValue==1)
        {
            self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"pLLetter" cacheName:nil];
        }
        else if(sortValue==2)
        {
            self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"pFLetter" cacheName:nil];
        }
        else if(sortValue==3)
        {
            self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"pSLetter" cacheName:nil];
        }

        self.fetchedResultsController.delegate = self;

        if (![[self fetchedResultsController] performFetch:&error]) NSLog(@"Error: %@", [error localizedDescription]);

        [self.tableView reloadData];
    }

    - (void)searchBarCancelButtonClickedUISearchBar *)searchBar
    {
        [self.searchDisplayController.searchBar setText:@""]; 
        [self performFetch];
    }

    - (void)searchBarUISearchBar *)searchBar textDidChangeNSString *)searchText
    {    
        [self performFetch];
    }
    #pragma mark - Table View

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (NSString *)tableViewUITableView *)tableView titleForHeaderInSectionNSInteger)section 
{
   id <NSFetchedResultsSectionInfo> sectionInfo = [[__fetchedResultsController sections] objectAtIndex:section];

        return [NSString stringWithFormat:@"%@", [sectionInfo name]];
}

- (NSInteger)tableViewUITableView *)tableView sectionForSectionIndexTitleNSString *)title atIndex:(NSInteger)index {

    if (index == 0) {
        // search item
        [tableView scrollRectToVisible:[[tableView tableHeaderView] bounds] animated:NO];
        return -1;
    }   
        return index-1;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{
    // Return the array of section index titles
    NSArray *searchArray = [NSArray arrayWithObject:UITableViewIndexSearch];
    return [searchArray arrayByAddingObjectsFromArray:self.fetchedResultsController.sectionIndexTitles];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        if(subtitleValue==1){cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];}
        else {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
        //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}



Best Answer-推荐答案


我遇到了同样的问题,这解决了它:在“performFetch()”中搜索结束时,不要调用 tableView.reloadData(),而是调用

    [self.searchDisplayController.searchResultsTableView reloadData];

原因:搜索后,有一个新的tableview,看这里:UITableView Plain Style Section Header gets Redrawn on Search View .

关于ios - UISearchBar 在搜索结果中显示多个部分标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25255954/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap