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

Ios UISearchBar在搜索结果中显示多个节标题,ios,uitableview,ios7,uisearchbar,uisearchbardisplaycontrol,Ios,Uitableview,Ios7,Uisearchbar,Uisearchbardisplaycontrol,我有一个应用程序,当有人使用搜索栏过滤UITableView中的对象时,会显示两个部分标题。一个是静止的,另一个随着搜索结果移动。请参阅附加的两个屏幕截图 下面我已经包括了我的应用程序中的tableview方法,任何帮助或建议都将不胜感激 - (void) performFetch { [NSFetchedResultsController deleteCacheWithName:@"Master"]; // Init a fetc

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

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

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

-(无效)执行蚀刻
{
[NSFetchedResultsController deleteCacheWithName:@“主控”];
//初始化获取请求
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
NSEntityDescription*entity=[NSEntityDescription entityForName:@“MainObject”在managedObjectContext:self.managedObjectContext]中;
[FetchRequestSetEntity:entity];
//对颜色项应用升序排序
//NSSortDescriptor*sortDescriptor=[[NSSortDescriptor alloc]initWithKey:@“Term”升序:是选择器:nil];
NSSortDescriptor*sortDescriptor;
if(sortValue==1)
{
sortDescriptor=[[NSSortDescriptor alloc]initWithKey:@“全名”升序:是选择器:@selector(不区分大小写比较:)];
}
else if(sortValue==2)
{
sortDescriptor=[[NSSortDescriptor alloc]initWithKey:@“firstName”升序:是选择器:@选择器(不区分大小写比较:)];
}
else if(sortValue==3)
{
sortDescriptor=[[NSSortDescriptor alloc]initWithKey:@“socialSecurity”升序:是选择器:@selector(不区分大小写比较:)];
}
NSArray*描述符=[NSArray数组WithObject:sortDescriptor];
[获取请求集合描述符:描述符];
//恢复查询
NSString*query=self.searchDisplayController.searchBar.text;
如果(query&&query.length)fetchRequest.predicate=[NSPredicate predicateWithFormat:@”(全名包含[cd]@)| |(社会安全包含[cd]@)”,则查询,查询;
//初始化获取的结果控制器
n错误*错误;
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;
如果(![[self-fetchedResultsController]performFetch:&error])NSLog(@“error:%@,[error localizedDescription]);
[self.tableView重载数据];
}
-(无效)搜索栏取消按钮选中:(UISearchBar*)搜索栏
{
[self.searchDisplayController.searchBar setText:@”“;
[自执行蚀刻];
}
-(无效)搜索栏:(UISearchBar*)搜索栏文本更改:(NSString*)搜索文本
{    
[自执行蚀刻];
}
#pragma标记-表视图
//自定义表视图中的节数。
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回[[self.fetchedResultsController节]计数];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
返回[sectionInfo numberOfObjects];
}
-(NSString*)表格视图:(UITableView*)表格视图标题标题标题部分:(NSInteger)部分
{
id sectionInfo=[[uuuu fetchedResultsController节]objectAtIndex:section];
返回[NSString stringWithFormat:@“%@,[sectionInfo name]];
}
-(NSInteger)tableView:(UITableView*)分区索引的tableView部分exttitle:(NSString*)标题索引:(NSInteger)索引{
如果(索引==0){
//搜索项
[tableView scrollRectToVisible:[[tableView tableHeaderView]边界]动画:否];
返回-1;
}   
收益指数-1;
}
-(NSArray*)sectionIndexTitlesForTableView:(UITableView*)表格视图
{
//返回节索引标题的数组
NSArray*searchArray=[NSArray arrayWithObject:UITableViewIndexSearch];
返回[searchArray arrayByAddingObjectsFromArray:self.fetchedResultsController.sectionIndexTitles];
}
//自定义表格视图单元格的外观。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
if(subtitleValue==1){cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier];}
else{cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
//cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
[self-configureCell:cell-atIndexPath:indexPath];
返回单元;
}

您只需要设置一个节而不是“return[[self.fetchedresultscoontroller sections]count];”

您只需要设置一个节而不是“return[[self.fetchedresultscoontroller sections]count];“

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

    [self.searchDisplayController.searchResultsTableView reloadData];

原因:搜索后,出现了一个新的tableview,请参见此处:。

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

    [self.searchDisplayController.searchResultsTableView reloadData];