4

I have a UITableViewController, with its table view having static cells, defined in a storyboard.

My table view has two sections. The first with two cells, and the second section has three cells. The second section also has text in its header.

What I would like to do is that when the user taps the first or second cells in the first section, to update the header text of the second section. Do so dynamically and with dynamic content (say the date and time is displayed there as of the moment they tap cells).

I have tried numerous things, but the viewForHeaderSection is only called once.

I registered the header section cell with

tableView.registerClass(TableSectionHeader.self, forHeaderFooterViewReuseIdentifier: "secondSectionCell")

Where TableSectionHeader is simply:

class TableSectionHeader: UITableViewHeaderFooterView { }

I am then able to dynamically set the section header, like so:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 1 {
        if let cell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("secondSectionCell") {
            cell.textLabel?.text = "hello world"
            return cell
        }

    }

    return nil
}

I also have implemented the following override, since some people suggest that when implementing viewForHeaderInSection, it is also required:

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 40.0
}

Even still. viewForHeaderInSection is only called once.

Am I able to somehow refresh the section header text dynamically as described above?

8
  • 2
    You can not do it. There is no official documentation for this. You need to do custom methods, like : stackoverflow.com/questions/23501952/… , however, I would strongly suggest you leave the section headers alone, and use cells in their place instead as the first cell in each section. Unless you want to do some sticky headers or other stuff that you really need.
    – user5890979
    Feb 9, 2017 at 3:18
  • @Sneak I like your suggestion. Probably the easiest way to go. The only thing is fiddling around with the table view to deal with the lines that separate cells, and how they are inset from the left edge of the tableview. Feb 9, 2017 at 3:40
  • tableView.reloadSections will reload the header (and footer) as well as the content for that section
    – Dale
    Feb 9, 2017 at 3:43
  • @dbarros it's really not that much of fiddling , actually much easier and customizable. just add register a XIB , subclass the Cell, and use Autolayout, you can customize everything 100% . If you want spacing, you can simply add borders in the XIB with Autolayout etc. customize bordercolors, everything you need. :)
    – user5890979
    Feb 9, 2017 at 3:49
  • 1
    @Dale Well, that isn't correct, if you reload the cells, your cellForRow will get called together with the rest of your datasource and delegates. If you have a UITextField in the cell for example, and the user have a current input, the input will be nilled out. If you have a movie/video playing, it will reload. If you have an image set from an NSURL, it will reload... and so on etc. :) it's all from case-to-case , if it works for your case your answer is 100% valid, however, I think the OP have some content that he dont want to reload. therefore he only wants to reload the specific section.
    – user5890979
    Feb 9, 2017 at 4:33

2 Answers 2

2

You can actually achieve this using traditional table view way easily.

Even though it is static UITableView, in your dataSource view controller, you can still implement - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

So how do you update the title on the fly? Create a property for this view controller, say NSString *titleForSecondSection. Whenever user tap the cells in the first section, you just need to update this property in the callback - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

The last step is to call [self.tableView reload] after you modified the titleForSecondSection property. Or if you don't want to reload the whole table view, just call - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

To be clear, in your - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section, for sections that don't need to change title, just return a static string. For sections that need to change title, return the dynamic property you created.

1
  • 1
    I'm sorry, I just noticed you are using Swift. Should've post in Swift. But it's the same idea. Hope it helps. :)
    – Elvin
    Feb 9, 2017 at 4:19
0

viewForHeaderInSection would only be called when the tableview is reloaded. So assuming you don't want to reload the whole tableview, you might need to change the content of label directly. pseudo codes like:

var label_first_section_header //as global variable

then in viewForHeaderInSection just point it to the label

if section == 1 {
    if let cell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("secondSectionCell") {
        cell.textLabel?.text = "hello world"
        label_first_section_header = cell.textLabel
        return cell
    }
}

then you can change the text dynamically whenever you want, for example in didSelectRowAtIndexPath

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.