Advertisement
Don_Mag

Section Headers

Dec 18th, 2022
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.76 KB | None | 0 0
  1. // Using a UITableViewController
  2.  
  3. class SecHeadTVC: UITableViewController {
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.        
  8.         self.title = "Section Header TableViewController"
  9.  
  10.         tableView.register(UITableViewCell.self, forCellReuseIdentifier: "c")
  11.        
  12.         if #available(iOS 15.0, *) {
  13.             tableView.sectionHeaderTopPadding = 0.0
  14.         }
  15.        
  16.     }
  17.  
  18.     override func numberOfSections(in tableView: UITableView) -> Int {
  19.         return 5
  20.     }
  21.     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  22.         return section + 1
  23.     }
  24.     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  25.         let c = tableView.dequeueReusableCell(withIdentifier: "c", for: indexPath)
  26.         c.textLabel?.text = "\(indexPath)"
  27.         c.textLabel?.backgroundColor = .cyan
  28.         c.contentView.backgroundColor = .yellow
  29.         return c
  30.     }
  31.    
  32.     override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  33.         let v = UILabel()
  34.         v.text = "Header for section: \(section)"
  35.         v.backgroundColor = .green
  36.         return v
  37.     }
  38. }
  39.  
  40. // Using a UITableView as a Subview of a UIViewController
  41.  
  42. class SecHeadVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
  43.    
  44.     override func viewDidLoad() {
  45.         super.viewDidLoad()
  46.  
  47.         self.title = "Section Header TableView as Subview"
  48.         view.backgroundColor = .systemYellow
  49.        
  50.         let tv = UITableView()
  51.         tv.translatesAutoresizingMaskIntoConstraints = false
  52.         view.addSubview(tv)
  53.        
  54.         let g = view.safeAreaLayoutGuide
  55.        
  56.         NSLayoutConstraint.activate([
  57.            
  58.             tv.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
  59.             tv.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
  60.             tv.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
  61.             tv.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
  62.            
  63.         ])
  64.  
  65.         tv.register(UITableViewCell.self, forCellReuseIdentifier: "c")
  66.         tv.dataSource = self
  67.         tv.delegate = self
  68.        
  69.         if #available(iOS 15.0, *) {
  70.             tv.sectionHeaderTopPadding = 0.0
  71.         }
  72.        
  73.     }
  74.    
  75.     func numberOfSections(in tableView: UITableView) -> Int {
  76.         return 5
  77.     }
  78.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  79.         return section + 1
  80.     }
  81.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  82.         let c = tableView.dequeueReusableCell(withIdentifier: "c", for: indexPath)
  83.         c.textLabel?.text = "\(indexPath)"
  84.         c.textLabel?.backgroundColor = .cyan
  85.         c.contentView.backgroundColor = .yellow
  86.         return c
  87.     }
  88.    
  89.     func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  90.         let v = UILabel()
  91.         v.text = "Header for section: \(section)"
  92.         v.backgroundColor = .green
  93.         return v
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement