Search code examples
iosobjective-ciphonesearchbar

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


IF Name is present then it will look like this

IF Name is not present then it look like this i want to hide section title if name is not present

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

I use this code but i don't want section title if name is not present , now its give me all section titles


Solution

  • If there is no rows than set the title of the section to nil.

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

    This will work.