MarkupKit 3.1 Released

MarkupKit 3.1 is now available for download. This release improves selection management in table and picker views by allowing callers to get and set selection state by value, similar to a <select> element in HTML.

For example, a web page that allows a user to choose one of several size options might include something like the following:

<select name="size">
    <option value="S">Small</option>
    <option value="M">Medium</option>
    <option value="L">Large</option>
    <option value="XL">Extra-Large</option>

Alternatively, the options could be presented using radio buttons like this:

<input type="radio" name="size" value="S"/>Small<br/>
<input type="radio" name="size" value="M"/>Medium<br/>
<input type="radio" name="size" value="L"/>Large<br/>
<input type="radio" name="size" value="XL"/>Extra-Large<br/>

In MarkupKit, this can be implemented using LMTableView as shown below:

<LMTableView style="groupedTableView">
    <?sectionName sizes?>
    <?sectionSelectionMode singleCheckmark?>
    <UITableViewCell textLabel.text="Small" value="S"/>
    <UITableViewCell textLabel.text="Medium" value="M"/>
    <UITableViewCell textLabel.text="Large" value="L"/>
    <UITableViewCell textLabel.text="Extra-Large" value="XL"/>

The value associated with the currently selected row can be retrieved using the valueForSection: method of LMTableView. This method takes a single argument containing the section index, which can be obtained via the sectionWithName: method. Rows can be programmatically selected using setValue:forSection::

let value = tableView.value(forSection: tableView.section(withName: "sizes")) as! String

tableView.setValue("L", forSection: tableView.section(withName: "sizes"))

Picker Views

Alternatively, size selection could be implemented using a picker view:

<LMPickerView id="pickerView">
    <?componentName sizes?>
    <row title="Small" value="S"/>
    <row title="Medium" value="M"/>
    <row title="Large" value="L"/>
    <row title="Extra-Large" value="XL"/>

In this case, the value associated with the selected row can be retrieved using the valueForComponent: method of LMPickerView, and rows can be programmatically selected using setValue:forComponent:. The component index can be obtained via componentWithName::

let value = pickerView.value(forComponent: pickerView.component(withName: "sizes")) as! String

pickerView.setValue("L", forComponent: pickerView.component(withName: "sizes"), animated: false)

Multiple Selection

In HTML, multiple options are often presented using a multi-select list element as shown below:

<select name="pets" size="5" multiple>
    <option value="D">Dog</option>
    <option value="C">Cat</option>
    <option value="F">Fish</option>
    <option value="R">Rabbit</option>
    <option value="T">Turtle</option>

Alternatively, the options may be represented by a group of checkboxes:

<input type="checkbox" name="pets" value="D"/>Dog<br/>
<input type="checkbox" name="pets" value="C"/>Cat<br/>
<input type="checkbox" name="pets" value="F"/>Fish<br/>
<input type="checkbox" name="pets" value="R"/>Rabbit<br/>
<input type="checkbox" name="pets" value="R"/>Turtle<br/>

In MarkupKit, this can also be implemented using LMTableView:

<LMTableView style="groupedTableView">
    <?sectionName pets?>
    <?sectionSelectionMode multipleCheckmarks?>
    <UITableViewCell textLabel.text="Dog" value="D"/>
    <UITableViewCell textLabel.text="Cat" value="C"/>
    <UITableViewCell textLabel.text="Fish" value="F"/>
    <UITableViewCell textLabel.text="Rabbit" value="R"/>
    <UITableViewCell textLabel.text="Turtle" value="T"/>

The values associated with the selected rows can be obtained using the valuesForSection: method, and set using setValues:forSection::

let values = tableView.values(forSection: tableView.section(withName: "pets")) as! [String]

tableView.setValues(["C", "T"], forSection: tableView.section(withName: "pets"))

Additional Information

For more information, please see the project README.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s