From 8a6f10c46eb10852634e04f769fd314989c03b8d Mon Sep 17 00:00:00 2001 From: Mike Silvers Date: Mon, 15 Apr 2019 10:10:30 -0400 Subject: [PATCH 1/2] Added the ability to select multiple items on the dropdown list. --- README.md | 14 ++++++++++ SearchTextField/Classes/SearchTextField.swift | 26 +++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 83619d8..adbbf22 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,20 @@ 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 diff --git a/SearchTextField/Classes/SearchTextField.swift b/SearchTextField/Classes/SearchTextField.swift index 2f6ac23..e2e6916 100755 --- a/SearchTextField/Classes/SearchTextField.swift +++ b/SearchTextField/Classes/SearchTextField.swift @@ -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 @@ -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() + } + } + } //////////////////////////////////////////////////////////////////////// From 9f5efe24bcdbc330270421a42940f8882c210fb7 Mon Sep 17 00:00:00 2001 From: Mike Silvers Date: Mon, 15 Apr 2019 10:12:34 -0400 Subject: [PATCH 2/2] Minor revisions to the readme file --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index adbbf22..dba80fa 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,9 @@ 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 + +// 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]