首页 > 解决方案 > 在 tapGesture 上更新 UITableViewHeaderFooterView 的标签

问题描述

我正在开发可折叠-可扩展的 Tableview 单元格,在该单元格中我为 UITableViewHeaderFooterView 创建了单独的类和 xib,并将两者相互链接。Xib 视图包含两个标签,一个用于标题,一个用于+ 或 -以指示部分已折叠或展开。现在在我的一个视图控制器中,我已经编写了所有必要的 tableview 方法,下面是相同的代码。

var sections = [
    Section(Title: "Sec 1", Indicator : "-" , expanded : true, moview : ["Simba1","TaleSpin1"]),
    Section(Title: "Sec 2", Indicator : "+" ,expanded : false, moview : ["Simba2","TaleSpin2"]),
    Section(Title: "Sec 3", Indicator : "+" ,expanded : false, moview : ["Simba3","TaleSpin3"]),
    Section(Title: "Sec 4", Indicator : "+" ,expanded : false, moview : ["Simba4","TaleSpin4"])
]

var selectIndexPath: IndexPath!

override func viewDidLoad() {
    super.viewDidLoad();

    selectIndexPath = IndexPath(row: -1, section: -1)

    tableView.register(UINib(nibName: "HeaderView", bundle: nil), forHeaderFooterViewReuseIdentifier: "HeaderView");
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sections[section].movie.count
}

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

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if sections[indexPath.section].expanded {
        return 44
    } else {
        return 0
    }
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 2
}


override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    //let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ExpandableHeader") as! ExpandableHeaderView;
    let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! HeaderView
    headerView.customInit(title: sections[section].Title , indicator: sections[section].Indicator , section: section, delegate: self)


    return headerView
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell")!;
    cell.textLabel?.text = sections[indexPath.section].movie[indexPath.row];

    return cell
}

func toggleSection(header: HeaderView, section: Int) {
    print("expanded: \(!sections[section].expanded)");
    sections[section].expanded = !sections[section].expanded
    sections[section].Indicator = "+"

    tableView.beginUpdates()
    for i in 0 ..< sections[section].movie.count {
        tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
    }
    tableView.endUpdates()
}

我想根据用户点击时部分是折叠还是展开来更改标签+或-。我尝试sections[section].Indicator = "+"在函数中添加这一行toggleSection但没有用。

任何帮助将不胜感激。

标签: iosswiftuitableviewuitableviewsectionheader

解决方案


推荐阅读