5

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

johny kumar
  • 1,270
  • 2
  • 14
  • 24
Asmita
  • 477
  • 8
  • 20

3 Answers3

1

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.

vijay
  • 165
  • 1
  • 5
1

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]); 
     } 
}
tuledev
  • 10,177
  • 4
  • 29
  • 49
0

You should either use this one returning the correct number of sections :

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

Or this one returning CGFLOAT_MIN if there is no rows in the corresponding section :

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

Hope it helps.

Tom

Tom
  • 373
  • 3
  • 13