UISearchBar在搜索结果中显示多个节标题

时间:2014-08-12 04:03:53

标签: ios uitableview ios7 uisearchbar uisearchbardisplaycontrol

我有一个应用程序,当有人使用搜索栏过滤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 selector:@selector(caseInsensitiveCompare:)];
        }
        else if(sortValue==2)
        {
            sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
        }
        else if(sortValue==3)
        {
            sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"socialSecurity" ascending:YES selector:@selector(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)searchBarCancelButtonClicked:(UISearchBar *)searchBar
    {
        [self.searchDisplayController.searchBar setText:@""]; 
        [self performFetch];
    }

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {    
        [self performFetch];
    }
    #pragma mark - Table View

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

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

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

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

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)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;
}

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,并修复了它:在搜索&#34; performFetch()&#34;结束时,不要调用tableView.reloadData(),而是调用

    [self.searchDisplayController.searchResultsTableView reloadData];

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

答案 1 :(得分:1)

你只需要设置一个部分而不是&#34;返回[[self.fetchedResultsController sections] count];&#34;