|
8 | 8 |
|
9 | 9 | import UIKit
|
10 | 10 |
|
11 |
| -class ViewController: UIViewController { |
| 11 | +class ViewController: UITableViewController { |
12 | 12 |
|
| 13 | + |
| 14 | + convenience init() { |
| 15 | + |
| 16 | + self.init(style: UITableViewStyle.grouped) |
| 17 | + |
| 18 | + } |
| 19 | + |
13 | 20 | override func viewDidLoad() {
|
14 | 21 | super.viewDidLoad()
|
15 |
| - // Do any additional setup after loading the view, typically from a nib. |
16 |
| - } |
17 | 22 |
|
18 |
| - override func didReceiveMemoryWarning() { |
19 |
| - super.didReceiveMemoryWarning() |
20 |
| - // Dispose of any resources that can be recreated. |
| 23 | + self.navigationItem.title = "Gun details" |
| 24 | + |
21 | 25 | }
|
22 | 26 |
|
23 | 27 |
|
| 28 | + |
| 29 | + |
| 30 | + override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { |
| 31 | + super.viewWillTransition(to: size, with: coordinator) |
| 32 | + |
| 33 | + // print("will execute before rotation") |
| 34 | + |
| 35 | + self.tableView.beginUpdates() |
| 36 | + |
| 37 | + coordinator.animate(alongsideTransition: { (context: UIViewControllerTransitionCoordinatorContext) in |
| 38 | + |
| 39 | + // print("will execute during rotation") |
| 40 | + |
| 41 | + self.tableView.endUpdates() |
| 42 | + |
| 43 | + }) { (context: UIViewControllerTransitionCoordinatorContext) in |
| 44 | + |
| 45 | + // print("will execute after rotation") |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + // *********************************************** |
| 53 | + // MARK: - UITableViewDataSource |
| 54 | + // *********************************************** |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + override func numberOfSections(in tableView: UITableView) -> Int { |
| 59 | + |
| 60 | + return 3 |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 66 | + |
| 67 | + return 5 |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 73 | + |
| 74 | + // since it is a static view we don't care about recycling at this time |
| 75 | + let result: UITableViewCell = UITableViewCell(style: .value1, reuseIdentifier: "cell") |
| 76 | + |
| 77 | + result.textLabel?.text = "Text for row at: \(indexPath.row)" |
| 78 | + result.detailTextLabel?.text = "Details for row at: \(indexPath.row)" |
| 79 | + |
| 80 | + return result |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { |
| 87 | + if section == 0 { |
| 88 | + let orientation = UIApplication.shared.statusBarOrientation |
| 89 | + |
| 90 | + if orientation.isLandscape { |
| 91 | + return view.frame.height |
| 92 | + } else { |
| 93 | + return view.frame.height / 3 |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return UITableViewAutomaticDimension |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { |
| 102 | + |
| 103 | + // We'll assume that there is only one section for now. |
| 104 | + |
| 105 | + if section == 0 { |
| 106 | + |
| 107 | + let imageView: UIImageView = UIImageView() |
| 108 | + imageView.clipsToBounds = true |
| 109 | + imageView.contentMode = .scaleAspectFill |
| 110 | + imageView.image = UIImage(named: "gun")! |
| 111 | + return imageView |
| 112 | + } |
| 113 | + |
| 114 | + return nil |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + |
24 | 119 | }
|
25 | 120 |
|
0 commit comments