如果在 ios8 中使用搜索栏找不到结果,如何隐藏章节标题

how to hide section title if result not found using searchbar in ios8

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (_segment.selectedSegmentIndex==0)
    {
        return sortedKeys;
    }
    else
    {
       return sortedKeys1;
    }
}

我使用此代码,但如果名称不存在我不想要章节标题,现在它给我所有章节标题

您应该使用这个返回正确数量的部分:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 

或者如果相应部分中没有行,则此返回 CGFLOAT_MIN:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

希望对您有所帮助。

汤姆

如果没有行,则将部分的标题设置为 nil。

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0)
 {
                return nil;
    } else {
        return "section title \(section)"
    }
   return @"";
}

这会起作用。

numberOfSectionsInTableView

中尝试 return 0
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

     if (result_not_found) { /// pass the condition when "result not found" here
        return 0;
     }

     if (_segment.selectedSegmentIndex==0) { 
        return ([sortedKeys count]); 
     } 
     else { 
        return ([sortedKeys1 count]); 
     } 
}