Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard is now automatically dismissed when an item is selected. #144

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ mySearchTextField.maxResultsListHeight = 200
mySearchTextField.highlightAttributes = [NSBackgroundColorAttributeName: UIColor.yellowColor(), NSFontAttributeName:UIFont.boldSystemFontOfSize(12)]

// Handle what happens when the user picks an item. By default the title is set to the text field
mySearchTextField.itemSelectionHandler = {item, itemPosition in
mySearchTextField.text = item.title
mySearchTextField.itemSelectionHandler = {filteredResults, itemPosition in
mySearchTextField.text = filteredResults[itemPosition].title
mySearchTextField.resignFirstResponder()
}

// You can force the results list to support RTL languages - Default: false
Expand Down Expand Up @@ -139,6 +140,9 @@ mySearchTextField.itemSelectionHandler = { filteredResults, itemPosition in

// Do whatever you want with the picked item
self.mySearchTextField.text = item.title

// Hide the keyboard as user has finished their selection
self.resignFirstResponder()
}

// Define a results list header - Default: nothing
Expand Down
6 changes: 6 additions & 0 deletions SearchTextField/Classes/SearchTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ open class SearchTextField: UITextField {
}
}

// Specify whether the keyboard should automatically dismiss when an item is selected
open var autoDismiss = true

/// Set an array of SearchTextFieldItem's to be used for suggestions
open func filterItems(_ items: [SearchTextFieldItem]) {
filterDataSource = items
Expand Down Expand Up @@ -595,6 +598,9 @@ extension SearchTextField: UITableViewDelegate, UITableViewDataSource {
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if itemSelectionHandler == nil {
self.text = filteredResults[(indexPath as NSIndexPath).row].title
if autoDismiss{
Copy link

@hydrated hydrated Nov 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A space before opening brass would be nicer.

self.resignFirstResponder()
}
} else {
let index = indexPath.row
itemSelectionHandler!(filteredResults, index)
Expand Down