Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 916
Asmita Avatar asked Oct 31 '22 17:10

Asmita


2 Answers

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.

like image 194
vijay Avatar answered Nov 15 '22 06:11

vijay


Try return 0 in numberOfSectionsInTableView

- (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]); 
     } 
}
like image 36
tuledev Avatar answered Nov 15 '22 06:11

tuledev