Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ mySearchTextField.forceNoFiltering = true
// Explicitly hide the results list
mySearchTextField.hideResultsList()

// Allow for multiple selections from the list. Default: false
mySearchTextField.allowMultipleSelections = true

// When using the delegate function with allowMultipleSelections = true,
// remember to add the new selection to the text
mySearchTextField.itemSelectionHandler = { filteredResults, itemPosition in
// Just in case you need the item position
let item = filteredResults[itemPosition]
// Add the selected item to the existing text
if let txt = self.mySearchTextField.text {
self.mySearchTextField.text = txt + " " + item.title
} else {
self.mySearchTextField.text = item.title
}


/**
* Update data source when the user stops typing.
* It's useful when you want to retrieve results from a remote server while typing
Expand Down
26 changes: 24 additions & 2 deletions SearchTextField/Classes/SearchTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ open class SearchTextField: UITextField {
////////////////////////////////////////////////////////////////////////
// Public interface

/// Allow for multiple selections
open var allowMultipleSelections = false

/// Maximum number of results to be shown in the suggestions list
open var maxNumberOfResults = 0

Expand Down Expand Up @@ -593,15 +596,34 @@ extension SearchTextField: UITableViewDelegate, UITableViewDataSource {
}

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

if itemSelectionHandler == nil {
self.text = filteredResults[(indexPath as NSIndexPath).row].title

if allowMultipleSelections {
if let st = self.text {
// this captures if there was another selection in the field
self.text = st + " " + filteredResults[(indexPath as NSIndexPath).row].title
} else {
// this is the first selection - there is nothing else in the field
self.text = filteredResults[(indexPath as NSIndexPath).row].title
}
} else {
// we are not using the multiple selections option
self.text = filteredResults[(indexPath as NSIndexPath).row].title
}

} else {
let index = indexPath.row
itemSelectionHandler!(filteredResults, index)
}

clearResults()
// this will only dismiss the table if allowMultipleSelections is not set
if !allowMultipleSelections {
clearResults()
}

}

}

////////////////////////////////////////////////////////////////////////
Expand Down