GithubHelp home page GithubHelp logo

aq12345 / multilevelmenu Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chokshen/multilevelmenu

0.0 2.0 0.0 1.48 MB

A custom multi-level menu/ address picker.

License: MIT License

Ruby 1.33% Swift 98.67%

multilevelmenu's Introduction

MultilevelMenu

A custom multi-level menu/ address picker.


gif1
MultilevelStyle1Menu

gif2
MultilevelStyle2Menu

Requirements

  • iOS 8+
  • Xcode 9+
  • Swift 4.0+

Installation

1.CocoaPods

pod 'MultilevelMenu'

2.Manually

Download the project, then drag the files of MultilevelMenu folder to your project.

Basic usage

1.Data structure

open class MenuDataModel {
    ///Id of a piece of data in menu,required
    open var id: String?
    ///Data name,required
    open var name: String?
    ///Data value,optional
    open var value: String?
    ///Id of last-level menu,required
    open var pid: String?
    ///Menu current level,optional
    open var level: Int?

    public init() {}

    public init(dict: [AnyHashable: Any]) {
        self.id = dict["id"] as? String
        self.name = dict["name"] as? String
        self.value = dict["value"] as? String
        self.pid = dict["pid"] as? String
        self.level = dict["level"] as? Int
    }
}

2.Init

Data handle

let array: [Dictionary<String, String>] = [
    ["id":"01","name":"实物产品行业","value":""],
    ["id":"02","name":"服务行业","value":""],
    ["id":"0101","name":"综合超市/卖场","pid":"01","value":""],
    ["id":"0102","name":"穿戴用品","pid":"01","value":""],
    ["id":"0103","name":"饮食产品","pid":"01","value":""],
    ["id":"0104","name":"房地产","pid":"01","value":""]
]

var dataSouce: [MenuDataModel] = []
for dict in array {
    let dataModel = MenuDataModel.init(dict: dict)
    dataSouce.append(dataModel)
}

Show

let menu = MultilevelStyle1Menu(title: "行业类型", dataSouce: dataSouce, completion:       { (resultString, model) in //'resultString' is combined with every level data that you have selected.'model' is the MenuDataModel that you have selected lastly.
    self.resultLabel.text = resultString
})
menu.show()

Important Property

menu.allowSelectAnyLevelData = true 
// It's false by defalut that it means you must select the last level data, otherwise you can't confirm result.But If it's true, meaning that you can select any level data to confirm result.

gif3
MultilevelStyle1Menu

gif4
MultilevelStyle2Menu

Custom Menu

  • Custom Cell
    Creat your custom cell by xib or code.
  • Create a MultilevelMenuStyle1View or MultilevelMenuStyle2View subclass
class CustomMenuView: MultilevelMenuStyle2View {
    // MARK: - UITableViewDelegate
    open override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as? CustomCell
        if cell == nil {
            cell = Bundle.main.loadNibNamed("CustomCell", owner: nil, options: nil)?.first as? CustomCell
        }
        cell?.contenLabel?.text = dataSouce[indexPath.row].name //Text
        cell?.iconImageView.image = UIImage(named: dataSouce[indexPath.row].value!) //Image

        //Selected properties
        cell?.tintColor = option.checkMarkColor
        if indexPath == lastSelectedIndexPath {
            cell?.accessoryType = .checkmark
        } else {
            cell?.accessoryType = .none
        }
        return cell!
    }
}

gif5

*Show

var option = MultilevelMenuOption()
option.rightBarButtonTitle = "ok"
option.rightBarButtonColor = UIColor.red

let menu = MultilevelStyle2Menu(title: "请选择行业类型", dataSouce: dataSouce, option: option, customView: CustomMenuView(), completion: { (resultString, model) in
    self.resultLabel.text = resultString
})
menu.show()

You also can creat MultilevelMenuOption to set menu custom properties.

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.