GithubHelp home page GithubHelp logo

bellaposa / apgenericsearchtextfield Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 238 KB

APGenericSearchTextField

License: MIT License

Ruby 6.78% Swift 93.22%
generics uitextfield uitableview keypath nspredicate

apgenericsearchtextfield's Introduction

APGenericSearchTextField

CI Status Version License Platform

Context

Swift 4 introduced a new type called KeyPath. It allows to access the properties of an object.
For instance:

let helloWorld = "HelloWorld" let keyPathCount = \String.count

let count = helloWorld[keyPath: keyPathCount]
//count == 10

The syntax can be very concise, it supports type inference and property chaining.

Purpose

Long time ago, I had the necessity to create a SearchTextField, and I thought it would be nice to apply this concept to it.

Under The Hood

  • KeyPath
  • NSPredicate

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Details

You have to follow few simple steps:

  1. Define your Model by subclassing NSObject
  2. Define property by using @objc
  3. Select filterOperator
  4. Select propertyToFilter
  5. Define your cellConfigurator

Model

class Person: NSObject {
	@objc let name: String
	@objc let surname: String

	init(name: String, surname: String) {
		self.name = name
		self.surname = surname
	}
}

Filter Operator

At the moment only two operators are supported

  1. Contains
  2. Equal

Property To Filter

Using keyPath you can choose what field you want to compare.

For istance: \.name if you want to filter your array objects by name

Cell Configurator

You can configure your cell in this way

searchTextField.cellConfigurator = { [weak self] (person, cell) in
			cell.textLabel?.text = person.name
}

Customization

It's possible to customize your suggestion TableView with these values

tableXOffset
tableYOffset
tableCornerRadius
tableBottomMargin
maxResultsListHeight
minCharactersNumberToStartFiltering

singleItemHandler will return the selected object in tableView

singleItemHandler = { [weak self] value in
		print(value)
}

Storyboard

Storyboards have a problem with having a generic class. The thing is that Interface Builder communicates to the ViewController through the Objective-C runtime. Because of this, InterfaceBuilder is limited to the features that Objective-C provides. In this case, generics are not supported. By the way there is a workaround to use generic class with storyboard.

You need to:

  • Declare a custom UITextField that extends GenericSearchTextField and add it to storyboard i.e. class SearchTextField: GenericSearchTextField<Person>{}

  • Define Outlet

    • Defining an outlet of type SearchTextField, XCode will report an error. It's important to change type from SearchTextField to UIView
    • Before: @IBOutlet weak var textField: SearchTextField!
    • After: @IBOutlet weak var textField: UIView!
  • 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
}
  • Use searchTextField variable

Bugs

The library is in alpha stage
Found a bug? Simple Pull Request

TODOs

  • Better test coverage
  • Better Filter management
  • Custom cell support
  • Storyboard Support
  • Swift Package Manager

Demo

Check out the Example project. You will find two examples with programmatically and storyboard.

Requirements

Installation

APGenericSearchTextField is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'APGenericSearchTextField'

Author

Bellaposa, [email protected]

License

APGenericSearchTextField is available under the MIT license. See the LICENSE file for more info.

apgenericsearchtextfield's People

Contributors

bellaposa avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

demysp

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.