iDeveloper iDeveloper - 3 months ago 31
Swift Question

Label automatically hide/show on scrolling in Header View

I am using expandable

tableView
, which is show/hide on click
sectionHeader
.

As I want to add
Label/Button
on
HeaderView
. but at the time of scrolling the label show/hide automatically. but I doesn't want label is disappear on scroll of
tableView
.

My Code is -

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


My Result

enter image description here

But I want Like This

enter image description here

When clicked on HeaderView it will Show like this

enter image description here

enter image description here

Answer

You need to add that small label with that background color inside viewForHeaderInSection method because with willDisplayHeaderView it will add that label multiple time, so change your viewForHeaderInSection and remove the willDisplayHeaderView method.

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
}

Edit: Change your last line of code that return the headerView like this

headerView.addSubview(label)
headerView.addSubview(labelValue)
headerView.addSubview(btnExpand)
headerView.sendSubviewToBack(label)
headerView.bringSubviewToFront(btnExpand)
return headerView