5

I have a project which uses Storyboards. I have a UITableView with Static cells and Group style.

I need to change the section text in one section depending on which selection is made in a segmented control (in another section)

I have found some solutions which indicate that you should use override this method:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

and trigger an update by calling:
[[self tableView]reloadSections:[NSIndexSet indexSetWithIndex:SectionToChange] withRowAnimation:UITableViewRowAnimationFade];

The problem is when I call reloadSections then all the rows and cells in the section in question get deleted. The text updates correctly thought but with this unwanted side effect.

  • Possible duplicate: http://stackoverflow.com/questions/10505708/how-to-set-the-uitableview-section-title-programmatically-iphone-ipad/10505982#10505982 – Alladinian Oct 13 '13 at 20:52
  • the question linked by @Alladinian has a cleaner solution. Just override `numberOfSectionsInTableView:` the titleForSection will be called, and since it's a static tableview you can just return the constant number that you know from your storyboard design – GreatWiz Oct 19 '14 at 09:03

3 Answers3

11

I think I found the answer here: Changing UITableView section header without tableView:titleForHeaderInSection

It may not be very elegant but it seams to work.

I can trigger an update to only the section header with none of the unwanted side effects by calling: [self.tableView headerViewForSection:1].textLabel.text = [self tableView:self.tableView titleForHeaderInSection:1];

So the only thing needed is to implement:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
   if (section == 1){
      if (self.segmentX.selectedSegmentIndex == 0) // or some condition
         return @"Header one";
      else
         return @"Header two";
  }
  return nil;
}
Community
  • 1
  • 1
  • 1
    This was a very good answer. I have this working without issue as far as iOS 7.1.1. – Bill Burgess May 01 '14 at 01:49
  • On iOS 8 I'm getting nil returned from headerViewForSection... To get round it I just cached up the header views my delegate was creating and referenced those instead. – CMash Jan 18 '15 at 20:02
  • How place text in lower case. Automatically change to uppercase. – jose920405 Feb 25 '16 at 21:50
-1

If you have implemented these functions then removing them should fix your problem:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Mike S
  • 11,329
  • 6
  • 41
  • 76
  • I'm currently implementing cellForRowAtIndexPath. Removing that function did not change the result. All rows and cell are removed in the section in question after calling: [[self tableView]reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade]; The section heading however changes correctly. – Ásgeir Gunnar Stefánsson Oct 14 '13 at 09:55
  • What about the numberOfRowsInSection method? There must be some underlying control method that is telling the section to have 0 cells after reloading the data. – Mike S Oct 15 '13 at 04:50
  • no it does not work. When I call [[self tableView]reloadSections:[NSIndexSet indexSetWithIndex:SectionToChange] withRowAnimation:UITableViewRowAnimationFade]; on the section in question the rows and cell just disappear leaving behind an empty gray space. The section header however changes correctly. – Ásgeir Gunnar Stefánsson Oct 15 '13 at 09:19
-3
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{        if (section == 0)
          {
                lbl.text = @"abc";
          }
        if (section == 2)
          {
                lbl.text = @"2ndsectionbeigns";
          }
return headerview;

}