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.

  • key

    As a presenter may present multiple SelectionViewControllers, 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 the setOptions(_:withDetails:sectionTitles:orderedAs:) method.

    Declaration

    Swift

    public private(set) var options = [NSObject:AnyObject]()
  • Supplementary info for entries in options. Keys should match those in options.

    Declaration

    Swift

    public private(set) var optionDetails = [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 the setOptions(_: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?
  • The current selections made by the user. These should be stored as the keys from the options and optionKeys. Selections can be pre-specified by the presenter as this property updates the selected rows in tableView on viewWillAppear:.

    Declaration

    Swift

    public var selectedKeys = [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 at indexPath in sortedOptionKeys.

  • 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 the options dictionary.

  • 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 Ints representing a section, with either a selected indexPath for that section or nil if there is none. There is a tuple for each section in tableView.

  • The error title shown if the user’s selection is invalid given selectionType and requiresSelection. - returns: The capitalized version of the title of this UIViewController.

    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 whether self.selectedKeys exceeds or does not meet the selection requirements.

  • The error title shown if the user’s selection is invalid given selectionType and requiresSelection.

    Declaration

    Swift

    public func errorDismissButtonTitle() -> String

    Return Value

    OK.