Dynamically change the section header text on a static cell

我只是一个虾纸丫 提交于 2019-12-11 06:17:43

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/42127531/dynamically-change-the-section-header-text-on-a-static-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!