|
7 | 7 | // |
8 | 8 |
|
9 | 9 | import UIKit |
| 10 | +import ReusableScrollMenuView |
10 | 11 |
|
11 | 12 | class ViewController: UIViewController { |
12 | 13 |
|
| 14 | + lazy var tableView: UITableView = { |
| 15 | + let tableView = UITableView(frame: .zero, style: .plain) |
| 16 | + tableView.delegate = self |
| 17 | + tableView.dataSource = self |
| 18 | + tableView.separatorStyle = .none |
| 19 | + tableView.estimatedSectionHeaderHeight = 50 |
| 20 | + tableView.scrollIndicatorInsets = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0) |
| 21 | + return tableView |
| 22 | + }() |
| 23 | + |
| 24 | + let menuTitles = ["Company", "Position", "Career", "Lifestyle", "Topic", "Longcat is very long"] |
| 25 | + var currentMenuIndex = 0 |
| 26 | + |
13 | 27 | override func viewDidLoad() { |
14 | 28 | super.viewDidLoad() |
15 | | - // Do any additional setup after loading the view, typically from a nib. |
| 29 | + |
| 30 | + addTableView() |
16 | 31 | } |
17 | 32 |
|
18 | 33 | override func didReceiveMemoryWarning() { |
19 | 34 | super.didReceiveMemoryWarning() |
20 | | - // Dispose of any resources that can be recreated. |
| 35 | + } |
| 36 | + |
| 37 | + func addTableView() { |
| 38 | + self.view.addSubview(tableView) |
| 39 | + |
| 40 | + tableView.register(UITableViewCell.self, forCellReuseIdentifier: "numberCell") |
| 41 | + tableView.register(ReusableScrollMenuView.self, forHeaderFooterViewReuseIdentifier: ReusableScrollMenuView.reuseIdentifier) |
| 42 | + |
| 43 | + tableView.snp.makeConstraints { (maker) in |
| 44 | + maker.edges.equalToSuperview() |
| 45 | + } |
21 | 46 | } |
22 | 47 |
|
23 | 48 | } |
24 | 49 |
|
| 50 | +extension ViewController: UITableViewDelegate, UITableViewDataSource { |
| 51 | + func numberOfSections(in tableView: UITableView) -> Int { |
| 52 | + return 1 |
| 53 | + } |
| 54 | + |
| 55 | + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 56 | + return 100 |
| 57 | + } |
| 58 | + |
| 59 | + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 60 | + let cell = tableView.dequeueReusableCell(withIdentifier: "numberCell", for: indexPath) |
| 61 | + cell.textLabel?.text = "\(indexPath.row)" |
| 62 | + return cell |
| 63 | + } |
| 64 | + |
| 65 | + func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { |
| 66 | + guard let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: ReusableScrollMenuView.reuseIdentifier) as? ReusableScrollMenuView else { return nil } |
| 67 | + view.bgColor = .systemBackground |
| 68 | + view.highlightColor = .systemOrange |
| 69 | + view.delegate = self |
| 70 | + view.dataSource = self |
| 71 | + return view |
| 72 | + } |
| 73 | + |
| 74 | + func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { |
| 75 | + return UITableViewAutomaticDimension |
| 76 | + } |
| 77 | + |
| 78 | + func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat { |
| 79 | + return 50 |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +extension ViewController: ReusableScrollMenuViewDelegate { |
| 84 | + func headerMenuView(_ headerMenuView: ReusableScrollMenuView, didSelectMenu index: Int) { |
| 85 | + guard menuTitles.count > index else { return } |
| 86 | + self.currentMenuIndex = index |
| 87 | + |
| 88 | + print("Menu \(index): \(menuTitles[index])") |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +extension ViewController: ReusableScrollMenuViewDataSource { |
| 93 | + func titlesForMenuView(_ menuView: ReusableScrollMenuView) -> [String] { |
| 94 | + return menuTitles |
| 95 | + } |
| 96 | + |
| 97 | + func selectedIndexForMenuView(_ menuView: ReusableScrollMenuView) -> Int? { |
| 98 | + return currentMenuIndex |
| 99 | + } |
| 100 | +} |
0 commit comments