SelectionViewController
public class SelectionViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
Simple class to display a list of choices.
The class generates cells using the identifiers Basic
or Detail
based on the values in self.optionDetails
. Cells should be registered against these identifiers to prevent exceptions being thrown. Options can be configured into multiple sections using nested arrays in the sortedOptionKeys
property. Cells should conform to SelectionCell
. Visual cell selection should be configured in the cell itself.
The delegate
property should be set to return the selected choice(s) on dismissal. The delegate methods only request dismissal, they makes no assumption of how to be dismissed. This allows for modal / push / custom / child view controller presentation of the choices.
-
As a presenter may present multiple
SelectionViewController
s, either singularly or at the same time, this property can be used to distinguish what this selection is for. It is not used in the class implementation.Declaration
Swift
public var key:Any?
-
Determines the auto-deselection behaviour. Default value is
.Single
.Declaration
Swift
public var selectionType:SelectionType = .Single
-
The table to show the selection.
Declaration
Swift
@IBOutlet public var tableView:UITableView?
-
Dictionary representing the options the user can choose from. The keys are ids used in the code, and will be passed back to the delegate as the selections parameter in the
selectionViewController:requestsDismissalWithSelections:
method. The values should be what is to be displayed to the user. This should be set from thesetOptions(_:withDetails:sectionTitles:orderedAs:)
method.Declaration
Swift
public private(set) var options = [NSObject:AnyObject]()
-
The titles to use for the section in the selection view. This should be set from the
setOptions(_:withDetails:sectionTitles:orderedAs:)
method.Declaration
Swift
public private(set) var sectionTitles:[String]? = nil
-
Array of Arrays of keys for the choices. The nested array represents the section - row structure of the tableview. the keys should be unique otherwise the user’s specific choices cannot be distinguished. These objects should be the same as the keys in
options
. This should be set from thesetOptions(_:withDetails:sectionTitles:orderedAs:)
method.Declaration
Swift
public private(set) var sortedOptionKeys = [[NSObject]]()
-
Determines whether or not the view can be dismissed without the user making a selection. If true and no selection has been made when the user requests dismissal, a UIAlertView is presented. Default value is
false
.Declaration
Swift
public var requiresSelection = false
-
Delegate to inform of the user’s requests for dismissal or cancel.
Declaration
Swift
public weak var delegate:SelectionViewControllerDelegate?
-
Updates properties.
Declaration
Swift
public override func viewDidLoad()
-
Pre-selects
tableView
cells based onselectedKeys
.Declaration
Swift
public override func viewWillAppear(animated: Bool)
-
Public setter for the data source properties. This sets all of the properties and reloads
tableView
.Declaration
Swift
public func setOptions(options:[NSObject:AnyObject], withDetails: [NSObject:AnyObject], sectionTitles:[String]?, orderedAs:[[NSObject]])
-
Helper method to map between the positions of options in the table and their keys in
options
.Declaration
Swift
public func keyForIndexPath(indexPath:NSIndexPath) -> NSObject
Parameters
indexPath
The index of the option whose key should be returned.
Return Value
A key in
options
which is atindexPath
insortedOptionKeys
. -
Helper method to map between the keys in
options
and the position of that option in a table.Declaration
Swift
public func indexPathForKey(key:NSObject) -> NSIndexPath?
Parameters
key
An object which is a key in
options
.Return Value
The index of this key in
sortedOptionKeys
, representing this option’s position in the table.nil
if the key given is not found in theoptions
dictionary.
-
Convenience method for getting
UITableView.reuseIdentifier
for a givenNSIndexPath
.Declaration
Swift
public func tableView(tableView:UITableView, cellIdentifierForRowAtIndexPath indexPath:NSIndexPath) -> String
Parameters
tableView
The
UITableView
to deque a cell from.indexPath
The path to get a reuse identifier for.
Return Value
Detail
ifoptionDetails
has an entry for the givenindexPath
,Basic
otherwise. -
Declaration
Swift
public func numberOfSectionsInTableView(tableView: UITableView) -> Int
Return Value
the number of sub arrays in
sortedOptionKeys
. -
Declaration
Swift
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
Return Value
the number of entries in the array at the given
indexPath.section
insortedOptionKeys
. -
Declaration
Swift
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
Return Value
A
UITableViewCell
dequeued based on the result oftableView(_:cellIdentifierForRowAtIndexPath:)
. If that cell conforms toSelectionCell
, the option and deatil are set on the label properties. -
Declaration
Swift
public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
Return Value
The entry in
sectionTitles
if it is set.
-
Manages selections. Deselects cells based on
selectionType
.Declaration
Swift
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
-
Updates
selectedKeys
based on the key for the givenindexPath
.Declaration
Swift
public func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
-
Convenience method for deselecting a cell.
Declaration
Swift
public func clearTableViewSelectionForIndexPath(indexPath:NSIndexPath)
-
Calculates the selected rows of
tableView
sorted by section.nil
objects are entered where there are no selections for a given section. This can be used to validate the user’s selection.Declaration
Swift
public func sectionedSelections() -> [(Int, [NSIndexPath])]
Return Value
An array of
Int
s representing a section, with either a selectedindexPath
for that section ornil
if there is none. There is a tuple for each section intableView
. -
The error title shown if the user’s selection is invalid given
selectionType
andrequiresSelection
. - returns: The capitalized version of the title of thisUIViewController
.Declaration
Swift
public func errorTitle() -> String?
Return Value
The capitalized version of the title of this
UIViewController
. -
The error message shown if the user’s selection is invalid.
Declaration
Swift
public func errorMessageForInvalidSelection() -> String
Return Value
An error message dependent
self.selectionType
whetherself.selectedKeys
exceeds or does not meet the selection requirements. -
The error title shown if the user’s selection is invalid given
selectionType
andrequiresSelection
.Declaration
Swift
public func errorDismissButtonTitle() -> String
Return Value
OK
.
-
Asks for cancellation from the delegate. No selection checking is done as it is assumed the previous selection is retained.
Declaration
Swift
public func cancelSelectionViewController(sender:AnyObject?)
-
Checks whether the view can be dismissed based on
selectionType
andrequiresSelection
. If it can dismissal is requested from the delegate otherwise aUIAlertController
is shown.Seealso
validSectionedSelection()
Seealso
errorTitle()
Seealso
errorMessageForInvalidSectionedSelection()
Seealso
errorDismissButtonTitle()
Declaration
Swift
public func dismissSelectionViewController(sender:AnyObject?)