动态更改静态单元格上的节标题文本 [英] Dynamically change the section header text on a static cell

查看:25
本文介绍了动态更改静态单元格上的节标题文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableViewController,它的表视图具有在故事板中定义的静态单元格.

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).

我尝试了很多东西,但 viewForHeaderSection 只调用了一次.

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

我用

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

其中 TableSectionHeader 就是:

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
    }

我也实现了以下覆盖,因为有人建议在实现viewForHeaderInSection时,也需要:

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
}

即使如此.viewForHeaderInSection 只调用一次.

Even still. viewForHeaderInSection is only called once.

我是否能够以某种方式动态刷新节标题文本,如上所述?

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

推荐答案

viewForHeaderInSection 只会在重新加载 tableview 时调用.因此,假设您不想重新加载整个 tableview,您可能需要直接更改 label 的内容.伪代码如:

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

然后在 viewForHeaderInSection 中将其指向标签

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
    }
}

然后您可以随时动态更改文本,例如在 didSelectRowAtIndexPath 中

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

这篇关于动态更改静态单元格上的节标题文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆