Skip to content

Search To Select View Controller

FUISearchToSelectViewController

open class FUISearchToSelectViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

A view controller which specializes in managing a tags field and a table view. Selected table view cell will appear in tags field with a designated tag. You can also deselect a table view cell by deleting its designated tag in tags field. In order to make this work, your subclass must implement FUISearchToSelectViewDelegate delegate.

Components

  • searchToSelectView
  • tagsField
  • tableView

Usage

Subclassing and conforms to delegate. It conforms to UITableViewDataSource and UITableViewDelegate by default.

class MyController: FUISearchToSelectViewController, FUiSearchToSelectViewDelegate {}

Override viewDidLoad and do some setup here

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.register(FUIObjectTableViewCell.self, forCellReuseIdentifier: FUIObjectTableViewCell.reuseIdentifier)
    self.tableView.register(FUITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: FUITableViewHeaderFooterView.reuseIdentifier)
    #if swift(>=4.2)
        self.tableView.rowHeight = UITableView.automaticDimension
    #else
        self.tableView.rowHeight = UITableViewAutomaticDimension
    #endif
    self.tableView.estimatedRowHeight = 50
    // ...
}

Implement dataSource and delegate methods

// Define data model.
var objects: [ModelObj] = []

// UITableView `dataSource` and delegate methods
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return objects.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: FUIObjectTableViewCell.reuseIdentifier, for: indexPath) as! FUIObjectTableViewCell
    let model = objects[indexPath.row]

    cell.isMomentarySelection = false
    cell.selectionStyle = .none
    cell.detailImage = model.image
    cell.detailImageView.isCircular = true
    cell.headlineText = model.headline
    cell.subheadlineText = model.subheadline
    cell.footnoteText = model.footnote

    return cell
}

// FUISearchtoSelectViewControllerDelegate delegate
func searchToSelectView(_ searchToSelectView: FUISearchToSelectView, uuidForItemAt indexPath: IndexPath) -> String? {
    let obj = objects[indexPath.row]
    return obj.uuid
}

func searchToSelectView(_ searchToSelectView: FUISearchToSelectView, tagTitleFor itemUUID: String) -> String? {
    if let obj = objects.filter ({ $0.uuid == itemUUID }).first {
        return obj.headline
    }
    return nil
}

Note: Developer is responsible for updating selectedUUIDs after data changes to make sure all selected UUIDs are still valid.

Theming

Supported class paths:

1
2
3
fdlFUISearchToSelectViewController_searchToSelectView {}
fdlFUISearchToSelectViewController_tableView {}
fdlFUISearchToSelectViewController_tagsField {}

Supported TagsField attributes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
tint-color (Color)
background-color (Color)
content-insets (Box)
font-color { -placeholder } (Color)
font-name (FontName)
font-style (UIFontTextStyle)
font-size (Number)
text-line-clamp (Integer)
line-spacing (Float)
tag-spacing (Float)
tag-delimiter (String)

Supported Tag attributes:

1
2
3
tag-font-color { -selected } (Color)
tag-background-color { -selected } (Color)
tag-content-insets (Box)

Supported ImageView attributes:

1
2
search-icon-image (Image)
search-icon-image-tint-color (Color)

Last update: January 15, 2021