标签在标题视图中滚动时自动隐藏/显示

时间:2016-08-29 05:59:16

标签: swift uitableview uiview

我正在使用展开式tableView,点按sectionHeader即可显示/隐藏。

我想在Label/Button上添加HeaderView。但在滚动标签时会自动显示/隐藏。但我不希望标签在tableView的滚动上消失。

我的代码是

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        // Background view is at index 0, content view at index 1
        if let bgView = view.subviews[0] as? UIView
        {
            if section == 0 {
                let labelValue = UILabel(frame: CGRect(x: 5, y: 80, width: 40, height: 20))
                labelValue.backgroundColor = UIColor.greenColor()
                bgView.addSubview(labelValue)

            }
            if section == 1{
                let labelValue = UILabel(frame: CGRect(x: 5, y: 80, width: 40, height: 20))
                labelValue.backgroundColor = UIColor.redColor()
                bgView.addSubview(labelValue)
            }
            if section == 2{
                let labelValue = UILabel(frame: CGRect(x: 5, y: 80, width: 40, height: 20))
                labelValue.backgroundColor = UIColor.yellowColor()
                bgView.addSubview(labelValue)
            }
            if section == 3{
                let labelValue = UILabel(frame: CGRect(x: 5, y: 80, width: 40, height: 20))
                labelValue.backgroundColor = UIColor.blueColor()
                bgView.addSubview(labelValue)
            }

        }

        view.layer.borderColor = UIColor.blackColor().CGColor
        view.layer.borderWidth = 0.3
    }

ViewForHeaderSection

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

    let headerView = HeaderView(tableView: self.customTableView, section: section)
    headerView.backgroundColor = UIColor.whiteColor()
    let label = UILabel(frame: headerView.frame)
    label.textAlignment = NSTextAlignment.Center
    label.font = UIFont (name: "HelveticaNeue-Light", size: 16)
    label.textColor = UIColor.blackColor()

    let labelValue = UILabel(frame: CGRect(x: 14, y: 15, width: 43, height: 32))
    labelValue.textAlignment = NSTextAlignment.Center
    labelValue.textColor = UIColor.whiteColor()
    labelValue.font = UIFont(name: "HelveticaNeue-Bold", size: 13)
    labelValue.text = "14"

    //let btnExpand = UIButton(frame: headerView.frame)
    let btnExpand = UIButton(frame:CGRect(x: 320, y: 17, width: 24, height: 24))
    btnExpand.setBackgroundImage(UIImage(named: "expand"), forState: .Normal)
    btnExpand.addTarget(self, action: #selector(HomeViewController.expandTableView), forControlEvents: .TouchUpInside)


    if section == 0 {
        label.text = "Runing Vehicle"
        labelValue.backgroundColor = UIColor(red: 14/255.0, green: 76/255.0, blue: 12/255.0, alpha: 1.0)
        btnExpand.addTarget(self, action: #selector(HomeViewController.expandTableView), forControlEvents: .TouchUpInside)

    }
    if section == 1 {
        label.text = "Idle Vehicle"
        labelValue.backgroundColor = UIColor(red: 148/255.0, green: 0/255.0, blue: 11/255.0, alpha: 1.0)

    }
    if section == 2 {
        label.text = "Vehicle AT POI"
        labelValue.backgroundColor = UIColor(red: 162/255.0, green: 136/255.0, blue: 5/255.0, alpha: 1.0)
    }
    if section == 3 {
        label.text = "All Vehicle"
        labelValue.backgroundColor = UIColor(red: 31/255.0, green: 61/255.0, blue: 127/255.0, alpha: 1.0)
    }
    headerView.addSubview(label)
    headerView.addSubview(labelValue)
    headerView.addSubview(btnExpand)
    return headerView


}

func expandTableView(){
    print("Hello")
    customTableView.contentSize = CGSizeMake(375, 667)
}

1 个答案:

答案 0 :(得分:1)

您需要在viewForHeaderInSection方法中添加带有背景颜色的小标签,因为willDisplayHeaderView会多次添加该标签,因此请更改viewForHeaderInSection并删除{{1方法。

willDisplayHeaderView

修改:更改返回headerView的最后一行代码,如下所示

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

    let headerView = HeaderView(tableView: self.customTableView, section: section)
    headerView.backgroundColor = UIColor.whiteColor()
    let label = UILabel(frame: headerView.frame)
    label.textAlignment = NSTextAlignment.Center
    label.font = UIFont (name: "HelveticaNeue-Light", size: 16)
    label.textColor = UIColor.blackColor()        
    let labelValue = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: headerView.frame.size.height))

    if section == 0 {
        label.text = "Runing Vehicle"
        labelValue.backgroundColor = UIColor.greenColor()
    }
    if section == 1 {
        label.text = "Idle Vehicle"
        labelValue.backgroundColor = UIColor.redColor()
    }
    if section == 2 {
        label.text = "Vehicle AT POI"
        labelValue.backgroundColor = UIColor.yellowColor()
    }
    if section == 3 {
        label.text = "All Vehicle"
        labelValue.backgroundColor = UIColor.blueColor()
    }
    headerView.addSubview(label)
    headerView.addSubview(labelValue)
    return headerView
}