I have added a HeaderView
(from .xib) to UITableView
,it is showing properly when i load the ViewController.But
how will i hide the tableView Header as i scroll up to any position of tableView
and show HeaderView when i scroll down.If anyone could help me it will be appreciated. Below is my code:
class ViewController: UIViewController,UITableViewDataSource,UIScrollViewDelegate {
@IBOutlet var myTable : UITableView!
var array = ["See All Local Deals","Food & Drink","Kids Activities","Taxi" , "Shopping" , "Local Tradesmen" , "Tattoo shop" , "Car Hire" , "Health & Fitness" , "Beauty & Spas" , "Home & Garden" , "Local Services" , "Nightlife" , "Wedding Planning" , "Holiday rentals"]
lazy var headerView : HeaderView? = {
return Bundle.main.loadNibNamed("HeaderView", owner: self, options: nil)?[0] as? HeaderView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.myTable.dataSource = self
self.myTable.tableHeaderView = headerView
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCell
cell.lblText.text = array[indexPath.row]
return cell
}
}
In storyboard-> Select your tableview-> go with property inspector-> And change type to Grouped from Plain
In Swift 5.0
The programatic way of doing this is to use UITableView.Style.grouped
:
self.tableView = UITableView(frame: .zero, style: .grouped)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With