Code Monkey home page Code Monkey logo

rsselectionmenu's Introduction

RSSelectionMenu

An elegant selection list or dropdown menu for iOS with single or multiple selections.

What's New

  • New Presentation Styles added: Alert & Actionshet

Alt text Alt text

Alt text Alt text

Features

Selection

Single | Multiple

Presentation Style

Present | Push | Formsheet | Popover | Alert | Actionsheet

Cell Style

Basic | RightDetail | SubTitle | Custom

Data Types

Premitive Types (String, Int,..) | Codable Objects | NSObject Subclasses | Dictionary Array

Customization

SearchBar | NavigationBar | Max Selection Limit | Header Row

Requirements

iOS 9.0+ | Xcode 8.3+ | Swift 3.0+

Installation

CocoaPods

pod 'RSSelectionMenu' or pod 'RSSelectionMenu', '~> 5.1'

Usage

Simple Selection List

let simpleDataArray = ["Sachin", "Rahul", "Saurav", "Virat", "Suresh", "Ravindra", "Chris"]
var simpleSelectedArray = [String]()

// Show menu with datasource array - Default SelectionType = Single
// Here you'll get cell configuration where you can set any text based on condition
// Cell configuration following parameters.
// 1. UITableViewCell   2. Object of type T   3. IndexPath

let selectionMenu =  RSSelectionMenu(dataSource: simpleDataArray) { (cell, object, indexPath) in
    cell.textLabel?.text = object
    
    // Change tint color (if needed)
    cell.tintColor = .orange
}

// set default selected items when menu present on screen.
// Here you'll get onDidSelectRow

selectionMenu.setSelectedItems(items: simpleSelectedArray) { (text, isSelected, selectedItems) in

    // update your existing array with updated selected items, so when menu presents second time updated items will be default selected.
    self.simpleSelectedArray = selectedItems
}

// show as PresentationStyle = Push
selectionMenu.show(style: .Push, from: self)

Multiple Selection List

let selectionMenu = RSSelectionMenu(selectionType: .Multiple, dataSource: dataArray, cellType: .Basic) { (cell, object, indexPath) in
    cell.textLabel?.text = object
}
  • Set Maximum selection limit (Optional)
selectionMenu.setSelectedItems(items: selectedDataArray, maxSelected: 3) { (text, selected, selectedItems) in
}

Presentation Style - Formsheet, Popover, Alert, Actionsheet

// show as Formsheet
selectionMenu.show(style: .Formsheet, from: self)

// show as Popover
selectionMenu.show(style: .Popover(sourceView: sourceView, size: nil), from: self)

// show as Alert
selectionMenu.show(style: .Alert(title: "Select", action: nil, height: nil), from: self)

// Show as Actionsheet
selectionMenu.show(style: .Alert(title: nil, action: "Done", height: nil), from: self)

Event Handlers

On Dismiss

selectionMenu.onDismiss = { selectedItems in
    self.selectedDataArray = selectedItems
    
    // perform any operation once you get selected items
}

On WillAppear

selectionMenu.onWillAppear = {
    /// do something..
}

Customization

SearchBar

  • You'll get notified via handler, when user starts typing in searchbar.
// show searchbar
selectionMenu.showSearchBar { (searchtext) -> ([String]) in

  // return filtered array based on any condition
  // here let's return array where name starts with specified search text

  return self.dataArray.filter({ $0.lowercased().hasPrefix(searchText.lowercased()) })
}

Cell Style - Right Detail or Sub Title

let selectionMenu = RSSelectionMenu(selectionType: .Single, dataSource: dataArray, cellType: .RightDetail) { (cell, object, indexPath) in

    // here you can set any text from object
    // let's set firstname in title and lastname as right detail

    let firstName = object.components(separatedBy: " ").first
    let lastName = object.components(separatedBy: " ").last

    cell.textLabel?.text = firstName
    cell.detailTextLabel?.text = lastName
}

selectionMenu.setSelectedItems(items: selectedDataArray) { (text, selected, selectedItems) in
    self.selectedDataArray = selectedItems
}

// show as default
selectionMenu.show(from: self)

Custom Cells

  • Provide custom cell with xib file name and cell identifier.
let selectionMenu =  RSSelectionMenu(selectionType: .Multiple, dataSource: customDataArray, cellType: .Custom(nibName: "CustomTableViewCell", cellIdentifier: "cell")) { (cell, person, indexPath) in

// cast cell to your custom cell type
let customCell = cell as! CustomTableViewCell

// set cell data here
}

Header Row - Empty, None, All, or Custom

// To show first row as Empty, when dropdown as no value selected by default
// Here you'll get Text and isSelected when user selects first row

selectionMenu.addFirstRowAs(rowType: .Empty, showSelected: self.firstRowSelected) { (text, isSelected) in
    
    // update your flag here to maintain consistency. -  This is required to be update when presenting for the second time.
    self.firstRowSelected = isSelected
}

DataSource - Codable Objects, NSObject Subclasses or Dictionary Array

  • Implement UniqueProperty protocol to model class or structure.
class Person: NSObject, UniqueProperty {

    let id: Int
    let firstName: String
    let lastName: String

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

    // Here id has the unique value for each person
    func uniquePropertyName() -> String {
        return "id"
    }
}

or

struct Employee: Codable, UniqueProperty {
    
    let empId: Int?
    let name: String?
    
    func uniquePropertyName() -> String {
        return "empId"
    }
}

or

selectionMenu.uniquePropertyName = "empId" or "keyname of unique value in dictionary"

UI Customization

NavigationBar

  • Set Title, BarButton Titles, TintColor, and Title Color
// set navigation title and color
selectionMenu.setNavigationBar(title: "Select Player", attributes: nil, barTintColor: UIColor.orange.withAlphaComponent(0.5), tintColor: UIColor.white)

// right barbutton title - Default is 'Done'
selectionMenu.rightBarButtonTitle = "Submit"

// left barbutton title - Default is 'Cancel'
selectionMenu.leftBarButtonTitle = "Close"

SearchBar

  • Set Placeholder, Tint Color
// show searchbar with placeholder and tint color
selectionMenu.showSearchBar(withPlaceHolder: "Search Player", tintColor: UIColor.withAlphaComponent(0.5)) { (searchtext) -> ([String]) in
    return self.dataArray.filter({ $0.lowercased().hasPrefix(searchtext.lowercased()) })
}

Example

See Example for more details.

License

RSSelectionMenu is released under the MIT license. See LICENSE for details.

rsselectionmenu's People

Contributors

rushisangani avatar amirnajafi avatar

Watchers

James Cloos avatar nitrodamsel 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.