swift - Label automatically hide/show on scrolling in Header View -


i using expandable tableview, show/hide on click sectionheader.

as want add label/button on headerview. @ time of scrolling label show/hide automatically. doesn't want label disappear on scroll of tableview.

my code -

func tableview(tableview: uitableview, willdisplayheaderview view: uiview, forsection section: int) {         // background view @ index 0, content view @ 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 @ 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) } 

you need add small label background color inside viewforheaderinsection method because willdisplayheaderview add label multiple time, change viewforheaderinsection , remove 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 @ 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 last line of code return headerview this

headerview.addsubview(label) headerview.addsubview(labelvalue) headerview.addsubview(btnexpand) headerview.sendsubviewtoback(label) headerview.bringsubviewtofront(btnexpand) return headerview 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -