A simple way to create a table view for settings, including:
- Table view cells with
UISwitch - Table view cells with center aligned text for tap actions
- A section that provides mutually exclusive options
- Actions performed when the row reacts to the user interaction
- Customizable table view cell image, cell style and cell accessory type
Set up tableContents in viewDidLoad:
import QuickTableViewController
class ViewController: QuickTableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableContents = [
Section(title: "Switch", rows: [
SwitchRow<SwitchCell>(title: "Setting 1", switchValue: true, action: { _ in }),
SwitchRow<SwitchCell>(title: "Setting 2", switchValue: false, action: { _ in }),
]),
Section(title: "Tap Action", rows: [
TapActionRow<TapActionCell>(title: "Tap action", action: { [weak self] in self?.showAlert($0) })
]),
Section(title: "Navigation", rows: [
NavigationRow(title: "CellStyle.default", subtitle: .none, icon: Icon(image: UIImage(named: "globe"), highlightedImage: UIImage(named: "globe-highlighted"))),
NavigationRow(title: "CellStyle", subtitle: .belowTitle(".subtitle"), icon: Icon(image: UIImage(named: "gear"))),
NavigationRow(title: "CellStyle", subtitle: .rightAligned(".value1"), icon: Icon(imageName: "time"), action: { [weak self] in self?.showDetail($0) }),
NavigationRow(title: "CellStyle", subtitle: .leftAligned(".value2"))
]),
RadioSection(title: "Radio Buttons", options: [
OptionRow(title: "Option 1", isSelected: true, action: nil),
OptionRow(title: "Option 2", isSelected: false, action: nil),
OptionRow(title: "Option 3", isSelected: false, action: nil)
], footer: "See RadioSection for more details.")
]
}
// MARK: - Actions
private func showAlert(_ sender: Row) {
// ...
}
private func showDetail(_ sender: Row) {
// ...
}
}NavigationRow(title: "UITableViewCellStyle.Default", subtitle: .none)
NavigationRow(title: "UITableViewCellStyle", subtitle: .belowTitle(".subtitle")
NavigationRow(title: "UITableViewCellStyle", subtitle: .rightAligned(".value1")
NavigationRow(title: "UITableViewCellStyle", subtitle: .leftAligned(".value2"))- Images in table view cells can be set by specifying the
iconof each row. - The
Iconstruct carries info about images for both normal and highlighted states. - Table view cells in
UITableViewCellStyle.value2will not show the image view.
NavigationRow(title: "Cell with image", subtitle: .none, icon: Icon(imageName: "icon"))- A
NavigationRowwith anactionwill be displayed in a table view cell whoseaccessoryTypeis.disclosureIndicator. - The
actionwill be invoked when the related table view cell is selected.
NavigationRow(title: "Navigation cell", subtitle: .None, action: { (sender: Row) in })- A
SwitchRowis representing a table view cell with aUISwitchas itsaccessoryView. - The
actionwill be invoked when the switch value changes. - The subtitle is disabled in
SwitchRow.
SwitchRow(title: "Switch", switchValue: true, action: { (sender: Row) in }),- A
TapActionRowis representing a button-like table view cell. - The
actionwill be invoked when the related table view cell is selected. Iconis disabled inTapActionRow.
TapActionRow(title: "Tap action", action: { (sender: Row) in })- An
OptionRowis representing a selectable table view cell. - When the row
isSelected, the table view cell shows.checkmarkas itsaccessoryType. - The
actionwill be invoked when the selection is toggled. - The subtitle is disabled in
OptionRow.
OptionRow(title: "Option", isSelected: true, action: { (sender: Row) in })OptionRowcan be used with or withoutRadioSection, which guarantees that there's only one option is selected.RadioSectionallows all options unselected by default. SettingalwaysSelectsOneOptionto true will preserve one selected option.selectedOptionis available as the result of selection inRadioSection.
All rows must conform to Row and RowStyle. Addtional interface to work with specific types of rows are represented as different protocols:
SwitchableTappableOptionSelectable
A customized table view cell type can be specified to rows during initialization.
// NavigationRow, using UITableViewCell if not specified.
NavigationRow<CustomCell>(title: "Navigation", subtitle: .none)
// SwitchRow, using SwitchCell if not specified.
SwitchRow<CustomSwitchCell>(title: "Switch", switchValue: true, action: { _ in })
// TapActionRow, using TapActionCell if not specified.
TapActionRow<CustomTapActionCell>(title: "Tap", action: { _ in })
// OptionRow, using UITableViewCell if not specified.
OptionRow<CustomOptionCell>(title: "Option", isSelected: true, action: { _ in })Since the rows carry different cell types, they can be matched using either the concrete types or the related protocol:
let action: (Row) -> Void = {
switch $0 {
case let option as OptionRow<CustomOptionCell>:
// only matches the option rows with a specific cell type
case let option as OptionSelectable:
// matches all option rows
default:
break
}
}Table view cell classes that conform to Configurable can implement additional configuration to set up the cell during tableView(_:cellForRowAt:):
protocol Configurable {
func configure(with row: Row & RowStyle)
}Other setups can also be added to each row using the customize closure:
protocol RowStyle {
var customize: ((UITableViewCell, Row & RowStyle) -> Void)? { get }
}The customization using register(_:forCellReuseIdentifier:) is deprecated after v0.6.0.
| QuickTableViewController | iOS | Xcode | Swift |
|---|---|---|---|
~> 0.1.0 |
8.0+ | 6.4 | |
~> 0.2.0 |
8.0+ | 7.0 | |
~> 0.3.0 |
8.0+ | 7.3 | |
~> 0.4.0 |
8.0+ | 8.0 | |
~> 0.5.0 |
8.0+ | 8.0 | |
~> 0.6.0 |
8.0+ | 8.3 | |
~> 0.7.0 |
8.0+ | 9.0 | |
~> 0.8.0 |
8.0+ | 9.1 |
Use CocoaPods
Create a Podfile with the following specification and run pod install.
platform :ios, '8.0'
use_frameworks!
pod 'QuickTableViewController'Use Carthage
Create a Cartfile with the following specification and run carthage update QuickTableViewController.
Follow the instructions to add the framework to your project.
github "bcylin/QuickTableViewController"
git submodule add -b master git@github.com:bcylin/QuickTableViewController.git Dependencies/QuickTableViewController
- Drag QuickTableViewController.xcodeproj to your app project as a subproject.
- On your application target's Build Phases settings tab, add QuickTableViewController-iOS to Target Dependencies.
QuickTableViewController is released under the MIT license. See LICENSE for more details. Image source: iconmonstr.
