Skip to content

Instantly share code, notes, and snippets.

@Bellaposa
Created June 6, 2020 14:02
Show Gist options
  • Save Bellaposa/133ca3aaa82f08396d980d0f414d137e to your computer and use it in GitHub Desktop.
Save Bellaposa/133ca3aaa82f08396d980d0f414d137e to your computer and use it in GitHub Desktop.
/// 1 - Declare your custom "UITextField" that extends GenericSearchTextField and add it to storyboard
class SearchTextField: GenericSearchTextField<Person> {}
class StoryboardExampleViewController: UIViewController {
/// 2: - DEFINE OUTLET
/// When creating outlet of type `SearchTextField` XCode will return an error
/// It's important to change TextField type from `SearchTextField` to `UIView`
@IBOutlet weak var textField: UIView!
/// 3: - DEFINE A COMPUTED PROPERTY of type `GenericSearchTextField`
/// cast your variable to `GenericSearchTextField`
/// Now your are able to use your variable
var searchTextField: GenericSearchTextField<Person> {
return textField as! GenericSearchTextField
}
var persons = Person.generateRandomPerson()
override func viewDidLoad() {
super.viewDidLoad()
searchTextField.placeholder = "Type Here"
searchTextField.filterOperator = .contains
searchTextField.propertyToFilter = \.name
searchTextField.cellConfigurator = { [weak self] (person, cell) in
cell.textLabel?.text = person.name
}
searchTextField.model = persons
searchTextField.singleItemHandler = { [weak self] value in
print(value)
self?.searchTextField.text = value.name
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment