├── DynamicUITableViewController.swift ├── LICENSE └── README.md /DynamicUITableViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DynamicUITableViewController.swift 3 | // QBabble 4 | // 5 | // Created by Keith Black on 3/16/16. 6 | // 7 | 8 | import UIKit 9 | 10 | class DynamicUITableViewController: UITableViewController { 11 | 12 | // MARK: Init functions 13 | override func viewDidLoad() { 14 | super.viewDidLoad() 15 | } 16 | 17 | override func didReceiveMemoryWarning() { 18 | super.didReceiveMemoryWarning() 19 | // Dispose of any resources that can be recreated. 20 | } 21 | 22 | 23 | // MARK: Override methods that allow hiding rows/sections 24 | 25 | // To control which sections/rows in your static TableViewController are hidden 26 | // you will need override one or both of these methods in your subclass. 27 | // NOTE: If you are hiding an entire section you do not have to implement shouldHideRow(), 28 | // it only needs to be implemented if you wish to hide specific rows within a section 29 | // without hiding the entire section. 30 | func shouldHideSection(section: Int) -> Bool { 31 | return false 32 | } 33 | 34 | func shouldHideRow(section: Int, row: Int) -> Bool { 35 | return false 36 | } 37 | // ---------------------------------------------------------- 38 | 39 | 40 | // MARK: Overrides that hide or show sections and rows. 41 | 42 | // These methods do the hiding based on the return value of 43 | // shouldHideSection() and shouldHideRow() above. 44 | // You don't need to override these methods in your code unless you 45 | // wish to do additional special handling with row/section height. 46 | 47 | // Hide section headers 48 | override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 49 | return shouldHideSection(section) ? 0.01 : super.tableView(tableView, heightForHeaderInSection: section) 50 | } 51 | 52 | // Hide section footers 53 | override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 54 | return shouldHideSection(section) ? 0.01 : super.tableView(tableView, heightForFooterInSection: section) 55 | } 56 | 57 | // Hide rows 58 | override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 59 | if shouldHideSection(indexPath.section) { 60 | return 0 61 | } 62 | if shouldHideRow(indexPath.section, row: indexPath.row) { 63 | return 0 64 | } 65 | return super.tableView(tableView, heightForRowAtIndexPath: indexPath) 66 | } 67 | 68 | // Hide header text by making it clear. 69 | // This seems unnecessary since the header will be hidden, however, if you don't do this 70 | // then pull the list down to reviel the space just above the top of the first section 71 | // you wil see all the header text drawn over top of itself. (xCode 7.3 & iOS 9.x) 72 | override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 73 | if shouldHideSection(section) { 74 | let headerView = view as! UITableViewHeaderFooterView 75 | headerView.textLabel!.textColor = UIColor.clearColor() 76 | } else { 77 | let headerView = view as! UITableViewHeaderFooterView 78 | headerView.textLabel!.textColor = UIColor.darkGrayColor() 79 | } 80 | } 81 | 82 | // Hide footer text by making clear 83 | override func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { 84 | if shouldHideSection(section) { 85 | let footerView = view as! UITableViewHeaderFooterView 86 | footerView.textLabel!.textColor = UIColor.clearColor() 87 | } else { 88 | let headerView = view as! UITableViewHeaderFooterView 89 | headerView.textLabel!.textColor = UIColor.darkGrayColor() 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 tkeithblack 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DynamicUITableViewController 2 | This Swift subclass of the iOS UITableViewController makes it easy to hide rows and/or sections in a static grouped UITableViewController. 3 | 4 | All you have to do is derive your TableViewController from DynamicUITableViewController and then in your subclass override the methods shouldHideSection(section: Int) and/or shouldHideRow(section: Int, row: Int). 5 | 6 | If you wish to hide a complete section you will return true from shouldHideSection(), if you wish to only hide a row you will return true from shouldHideRow(). 7 | --------------------------------------------------------------------------------