Skip to content

Creating Custom Schemes

James Richard edited this page Dec 19, 2015 · 11 revisions

You can make your own custom schemes by conforming to Scheme. These types must be classes. Some of the methods are implemented as default implementations for convenience.

Builders

All built-in schemes are typically created using a builder object. This provides a simple block-based structure to your table schemes. A builder conforms to SchemeBuilder, and are initialized using the init() method. This method is required, even if it doesn't do anything. This is to support a Swift language feature of template initialization.

When you conform to SchemeBuilder, you should also mark your init() as required.

SchemeBuilders run createScheme() to construct the Scheme. If there is an issue with the builder's configuration in your implementation you should throw an error in this method, but only if its a required bit of information. These are try!'d to alert implementors of issues early.

Configuring Cell

The TableScheme class will call public func configureCell(cell: UITableViewCell, withRelativeIndex relativeIndex: Int after obtaining a cell from the tableView dequeue method. You should override this method to call any configuration code your scheme supports. A common pattern with the built-in schemes is to let the user set a configurationHandler on the scheme when constructing the scheme, and it is called in this method. Some built-ins have multiple kinds of configuration handlers based on the state of the scheme.

Cell Selection

The TableScheme class will call public func selectCell(cell: UITableViewCell, inTableView tableView: UITableView, inSection section: Int, havingRowsBeforeScheme rowsBeforeScheme: Int, withRelativeIndex relativeIndex: Int) when the tableView(tableView: UITableView, didSelectIndexPath indexPath: NSIndexPath) method is called on it. This method is typically called by the UITableViewDelegate when it receives the UITableViewDelegate.tableView(tableView: UITableView, didSelectIndexPath indexPath: NSIndexPath).

You should add a selection handler to your custom scheme that is called when this method is called, similar to how the built-in schemes do. You're free to modify the UITableView to change how many cells your scheme is presenting, but you shouldn't add or remove ones being displayed to other schemes.

Height

The TableScheme class will call public func heightForRelativeIndex(relativeIndex: Int) -> RowHeight when the tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat method is called on it. This method is typically called by the UITableViewDelegate when it receives the UITableViewDelegate.tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat.

You should add either a specific height, an array of heights, or a closure to determine the height on your scheme, similar to how the built-in schemes do.

Questions

If you have any questions about creating custom schemes you can contact us. You can also refer to the built-in schemes as a reference. Happy coding!