GithubHelp home page GithubHelp logo

plummer / suggestionrow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from eurekacommunity/suggestionrow

0.0 1.0 0.0 150 KB

Eureka row that displays completion suggestions either below the row in a table view or in the input accessory view above the keyboard.

License: MIT License

Swift 95.26% Ruby 4.74%

suggestionrow's Introduction

Platform iOS

Swift 3 compatible CocoaPods compatible License: MIT

Contents

Introduction

SuggestionRow is a row extension for the Eureka iOS form builder that provides completion suggestions as the user is typing. There are two rows that can be instantiated:

  • SuggestionAccessoryRow<T> - provides completion suggestions as tags in a horizontal collectionView displayed as the inputAccessoryView above the keyboard.
  • SuggestionTableRow<T> - provides completion suggestions in a table below the cell.

Both classes are generic and can be used to provide completion suggestions for any type that conforms to the SuggestionValue protocol:

public protocol SuggestionValue: Equatable, InputTypeInitiable {
    var suggestionString: String { get }
}

SuggestionRow is based off of GooglePlacesRow

Usage

Conform to SuggestionValue protocol

First, the type that you want to provide completion suggestions for must conform to the SuggestionValue protocol. For example, consider the following Scientist struct:

struct Scientist {
    var id: Int
    var firstName: String
    var lastName: String

    init(id: Int, firstName: String, lastName: String) {
        self.id = id
        self.firstName = firstName
        self.lastName = lastName
    }
} 

To conform to the SuggestionValue protocol, add a suggestionString method that specifies how Scientists should be displayed as suggestions. This is the text that will either go in a tag in the inputAccessoryView or in a row in the suggestions UITableView. For Scientists, displaying the full name is a good option, as shown below. Scientist must also conform to Equatable and InputTypeInitiable as required by SuggestionValue. InputTypeInitiable is a protocol defined by Eureka which requires an optional initializer that takes in a String. It's used to determine how to instatiate a value from the contents of a UITextField. In this case, the text that the user types in is used to filter existing values but not create new ones so the initializer can return nil.

extension Scientist: SuggestionValue {
    // Required by `InputTypeInitiable`, can always return nil in the SuggestionValue context.
    init?(string stringValue: String) {
        return nil
    }
    
    // Text that is displayed as a completion suggestion.
    var suggestionString: String {
        return "\(firstName) \(lastName)"
    }
}

To conform to the Equatable protocol, implement the == operator at a global scope:

func == (lhs: Scientist, rhs: Scientist) -> Bool {
    return lhs.id == rhs.id
} 

Instantiate the row

In your form, add a row of type SuggestionAccessoryRow<Scientist> or SuggestionTableRow<Scientist>. You must define the row's filterFunction to determine where completion suggestions come from and how they are selected based on the user's input. The function you set as filterFunction must take in a String parameter (what the user has typed in) and return an array of Scientist (the valid suggestions). This function is called each time the user input field changes. The following example is also in SuggestionExampleViewController:

let users: [Scientist] = [Scientist(id: 1, firstName: "Albert", lastName: "Einstein"),
                          Scientist(id: 2, firstName: "Isaac", lastName: "Newton")]
...                          
form +++ Section("Table suggestions")
    <<< SuggestionTableRow<Scientist>() {
        $0.filterFunction = { [unowned self] text in
            self.users.filter({ $0.firstName.lowercased().contains(text.lowercased()) })
        }
        $0.placeholder = "Search for a famous scientist"
    }

Customization

SuggestionAccessoryRow<T>

SuggestionAccessoryRow uses a generic SuggestionCollectionCell cell whose generic parameter is the UICollectionViewCell class used in the inputAccessoryView.

  • If you want to make minor visual changes to the suggestion cells, use the customizeCollectionViewCell callback.

  • If you want to change the layout of the collectionView then you can use/modify/override the collectionViewLayout attribute in the cellSetup method when declaring the row.

  • If you want to change something about the collectionView (e.g. its height, backgroundColor) then you can also do that in the cellSetup method.

  • If you want to change the collection view cell of the inputAccessoryView drastically, create your own row (MySuggestionAccessoryRow) with your own cell class which conforms to EurekaSuggestionCollectionViewCell.

SuggestionTableRow

SuggestionTableRow uses a generic SuggestionTableCell cell whose generic parameter is the UITableViewCell class used to create the cells displayed in a UITableView with the suggested options.

  • If you want to make minor visual changes to the suggestion table row cells, use the customizeTableViewCell callback.

  • You can customize attributes of the tableView that displays the options. You should do this in cellSetup and keep in mind that the frame of the tableView is reset each time the tableView is displayed.

  • If you want to change these cells drastically, create your own row with your own cell class which conforms to EurekaSuggestionTableViewCell.

Dependencies

  • Eureka

Requirements

  • iOS 8.0+
  • Xcode 8.3+
  • Swift 3

Getting involved

  • If you want to contribute please feel free to submit pull requests.
  • If you have a feature request please open an issue.
  • If you found a bug or need help please check older issues before submitting an issue..

suggestionrow's People

Contributors

lognaturel avatar mats-claassen avatar brandons avatar

Watchers

Andrew Plummer avatar

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.