分层菜单 1.3.2

分层菜单 1.3.2

chokshen 维护。



  • shenzhiqiang

分层菜单

自定义多级菜单/地址选择器。


gif1
分层样式1菜单

gif2
分层样式2菜单

要求

  • iOS 8+
  • Xcode 10+
  • Swift 4.2+

安装

1.CocoaPods

pod 'MultilevelMenu'

2.手动

下载项目,然后将分层菜单文件夹的文件拖到您的项目中。

基本用法

1.数据结构

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.初始化

数据处理

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)
}

显示

  • Swift
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()
  • Objective-C
__weak typeof(self) weakSelf = self;
MultilevelStyle1Menu *menu = [[MultilevelStyle1Menu alloc]initWithTitle:@"行业类型" dataSouce:dataSource option:nil customView:nil completion:^(NSString * text, MenuDataModel * model) {
    weakSelf.resultLabel.text = text;
}];
[menu show];

重要属性

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
分层样式1菜单

gif4
分层样式2菜单

自定义菜单

  • 自定义单元格
    通过xib或代码创建自定义单元格。
  • 创建一个MultilevelMenuStyle1View或MultilevelMenuStyle2View子类
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

*(显示)

  • Swift
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()
  • Objective-C
MultilevelMenuOption *option = [[MultilevelMenuOption alloc]init];
option.rightBarButtonTitle = @"ok";
option.rightBarButtonColor = [UIColor redColor];
__weak typeof(self) weakSelf = self;
MultilevelStyle2Menu *menu = [[MultilevelStyle2Menu alloc] initWithTitle:@"请选择行业类型" fileUrl:url option:option customView:nil completion:^(NSString * text, MenuDataModel * model) {
    weakSelf.resultLabel.text = text;
}];
[menu show];

您还可以创建MultilevelMenuOption来设置菜单的自定义属性。