首页 > 解决方案 > 如何绑定 UITableView Header 并且还想通过在 swift 中以编程方式单击 tableview 标题来显示或隐藏 tableview 单元格

问题描述

我想在tableView header. 我已经尝试了不同的方式,但达到了目标。下面给出了我的代码,用于在headerView.

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return arrSupport[section]
}

func tableView(_ tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
    if let tableViewHeaderFooterView = view as? UITableViewHeaderFooterView {
        tableViewHeaderFooterView.textLabel?.textColor = UIColor.white
        tableViewHeaderFooterView.textLabel?.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        tableViewHeaderFooterView.contentView.backgroundColor = UIColor(red: 39.0 / 255.0, green: 41.0 / 255.0, blue: 53.0 / 255.0, alpha: 1.0)
        let headerView = UIView()
        headerView.frame = CGRect(x: tblSupport.frame.origin.x-10, y: 10, width: tblSupport.frame.size.width-20, height: 60)
        tableViewHeaderFooterView.contentView.frame = CGRect(x: 10, y: 0, width: tableView.frame.size.width-20, height: 50)
        headerView.addSubview(tableViewHeaderFooterView)
    }
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerView = UIView()
    headerView.frame = CGRect(x: tblSupport.frame.origin.x-10, y: 10, width: tblSupport.frame.size.width-20, height: 60)
    headerView.backgroundColor = UIColor(red: 39.0 / 255.0, green: 41.0 / 255.0, blue: 53.0 / 255.0, alpha: 1.0)
    return headerView
}` 

而且我想通过点击相应的headerview来显示或隐藏tableview单元格还需要减少headerview的宽度,比如期望状态

当前状态:

期望状态:

标签: iosswiftuitableviewuitableviewsectionheader

解决方案


首先要解决您的第一个问题,您需要指定标题的高度。

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

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView.init(frame: CGRect.init(x: 10, y: 10, width: tblSupport.frame.width - 20, height: 40)
    headerView.backgroundColor = UIColor(red: 39.0 / 255.0, green: 41.0 / 255.0, blue: 53.0 / 255.0, alpha: 1.0)

    return headerView

}

现在对于您的第二个问题,我们将使单元格高度最小化,并通过调用beginUpdates()and endUpdates()tableview将制作一个动画,使您的单元格以动画方式缩小:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView.init(frame: CGRect.init(x: 10, y: 10, width: tblSupport.frame.width - 20, height: 40)


    let myButton = UIButton.init(frame: headerView.frame)

    myButton.backgroundColor = .clear
    myButton.tag = section
    myButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)



    headerView.addSubview(myButton)

    return headerView

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {


    // add the next if statement

    if indexPath.section == chosenSection {
        return 0.0000001
    }

    return 30 // put your usual cell height here
}


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

var chosenSection = -1

@objc func buttonPressed(sender : UIButton) {


    if chosenSection == sender.tag {
        chosenSection = -1

    } else {
        chosenSection = sender.tag

        tblSupport.beginUpdates()
        tblSupport.endUpdates()
    }

}

注意:将行高设置为 0 不会使其为 0,但会将其设置为默认高度,这就是我写 0.000001 的原因


推荐阅读