在tapGesture上更新UITableViewHeaderFooterView的标签

时间:2018-06-12 09:55:00

标签: ios swift uitableview uitableviewsectionheader

我正在开发可折叠扩展的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这一行,但没有用。

任何帮助将不胜感激。

0 个答案:

没有答案
相关问题