Swift快速集成控件篇(DopDropDownMenu[二级菜单])第三方控件

我们先看看实现功能

Swift快速集成控件篇(DopDropDownMenu[二级菜单])第三方控件_第1张图片
二级菜单.gif

首先先感谢作者提供的源码

(https://github.com/12207480/DOPDropDownMenu-Enhanced)

本源码不是我写的,只是我在他源码的功能上进行了代码封装以便更直观


//需要继承已写好的类
class aadViewController: ScreeningViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad() 
        SelectedCallBackValue() //选中的必包回调
        
        let mol = MenuModel()
        
        for   i:Int in 0  ..< 25 {    //第一个菜单
            
            let Onemol=OneMenuModel()
            Onemol.name="区域\(i)"
            Onemol.value=i.description
            if(i<5){ 
                Onemol.image="aeroplane"
                for   l:Int in 0  ..< 50 {
                    let Towmol=TowMenuModel()
                    Towmol.name="朝阳\(l)"
                    Towmol.value=i.description
                    if(l==2){
                        Towmol.name="中心"
                        Towmol.image="aeroplane"
                        Towmol.value="123"
                    }
                    if(l==3){
                        Towmol.name="其他"
                        Towmol.image="add-circular"
                        Towmol.value="321"
                    }
                    
                    Onemol.TowMenu.append(Towmol)  //添加二级菜单值
                }
            }
            mol.OneMenu.append(Onemol)  
        }
        
        AddMenuData(mol)  //新增
        
        let mol1 = MenuModel()
        
        for   i:Int in 0  ..< 3 {    
            let Onemol=OneMenuModel()
            Onemol.name="价格\(i)"
            if(i>=1){
                Onemol.name="1000\(i)"
            }
            Onemol.value=i.description
            mol1.OneMenu.append(Onemol)
            
        }
        
        AddMenuData(mol1)
        let mol2 = MenuModel()
        for   i:Int in 0  ..< 3 {
            let Onemol=OneMenuModel()
            Onemol.name="户型\(i)"
            Onemol.value=i.description
            mol2.OneMenu.append(Onemol)
            
        }
        AddMenuData(mol2)
        let mol3 = MenuModel()
        for   i:Int in 0  ..< 3 {
            let Onemol=OneMenuModel()
            Onemol.name="面积\(i)  "
            Onemol.value=i.description
            mol3.OneMenu.append(Onemol)
        }
        AddMenuData(mol3)
        menureloadData()    //刷新菜单 (每次加载完数据后都需要刷新
        
    }
    
    
    func SelectedCallBackValue(){
        //选中的必包回调
        self.Callback_SelectedValue { (name, value) in
            print(name,value)
        }
    }

  override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

ScreeningViewController 类

 
import UIKit
import DOPDropDownMenu_Enhanced

class OneMenuModel {     //一级菜单属性
    ///标题名称(名称不可重复)
    var  name:String?
    ///值 id   (值不可重复)
    var  value:String?
    ///图片
    var  image:String?
    var  TowMenu = [TowMenuModel]()
    
}
class TowMenuModel {    //二级菜单属性
    ///标题名称(名称不可重复)
    var  name:String?
    ///值 id   (值不可重复)
    var  value:String?
    ///图片
    var  image:String?
}


class  MenuModel {      //对象集合
    var  OneMenu = [OneMenuModel]()
}

class ScreeningViewController: UIViewController,DOPDropDownMenuDataSource,DOPDropDownMenuDelegate {
    
    typealias CallbackSelectedValue=(name:String,value:String)->Void
    
    //声明一个闭包
    private  var myCallbackValue:CallbackSelectedValue?
    //下面这个方法需要传入上个界面的函数指针
    func  Callback_SelectedValue(value:CallbackSelectedValue?){
        //将函数指针赋值给闭
        myCallbackValue = value
    }
    
    var menu = DOPDropDownMenu()    // 菜单
    
    
    private var index=0 //第一次加载数据的索引
    private  var isLoadMenu=false    //是否加载菜单完成?
    
    private var  _MenuValue = [MenuModel]() //菜单数据模型
    
    func AddMenuData(model:MenuModel){
        _MenuValue.append(model)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        menu = DOPDropDownMenu(origin: CGPoint(x: 0,y:self.navigationController == nil ? 0:CommonFunction.NavigationControllerHeight), andHeight: 44)
        menu.isClickHaveItemValid=true
        menu.delegate=self
        menu.dataSource=self 
        self.view.addSubview(menu)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func menureloadData()  {
       menu.reloadData()
    }
    
    
    //返回当前显示的MenuTitle列总数
    func numberOfColumnsInMenu(menu: DOPDropDownMenu!) -> Int {
        return  _MenuValue.count
    }
    
    // 返回 menu 第column列有多少行  (一级
    func menu(menu: DOPDropDownMenu!, numberOfRowsInColumn column: Int) -> Int {
        if(column>=0){
            let value  = _MenuValue[column]
            return value.OneMenu.count
        }
        
        return 0
    }
    // 返回 menu 第column列 每行title (一级
    func menu(menu: DOPDropDownMenu!, titleForRowAtIndexPath indexPath: DOPIndexPath!) -> String! {
        if(isLoadMenu){
            //每当点击菜单的时候
            let title = _MenuValue[indexPath.column]
            if(title.OneMenu[indexPath.row].name=="" ||
                title.OneMenu[indexPath.row].name==nil){
                return "无数据"
            }
            else{
                return title.OneMenu[indexPath.row].name    //把点击菜单的OneMenu全部显示出来
            }
            
        }
        //第一次加载菜单的时候
        let title = _MenuValue[index]
        index+=1
        if(index==_MenuValue.count){
            isLoadMenu=true
        }
        return title.OneMenu[0].name    //添加数据显示 (显示第一个onemenu)
    }
    
    // 返回 menu 第column列有多少行  (二级
    func menu(menu: DOPDropDownMenu!, numberOfItemsInRow row: Int, column: Int) -> Int { 

  let value  = _MenuValue[column]
        if(value.OneMenu[row].TowMenu.count==0){
            return -1
        }
        return  value.OneMenu[row].TowMenu.count
        
        
    }
    // 返回 menu 第column列 每行title (二级
    func menu(menu: DOPDropDownMenu!, titleForItemsInRowAtIndexPath indexPath: DOPIndexPath!) -> String! {
        
        let title = _MenuValue[indexPath.column]
        if(title.OneMenu[indexPath.row].TowMenu[indexPath.row].name=="" ||
            title.OneMenu[indexPath.row].TowMenu[indexPath.row].name==nil){
            
            return "无数据"
        }
        else{
            let value = title.OneMenu[indexPath.row].TowMenu[indexPath.item].name   //把点击菜单的TowMenu全部显示出来
            return value
        }
        
    }
    //一级菜单图片(文字左边logo
    func menu(menu: DOPDropDownMenu!, imageNameForRowAtIndexPath indexPath: DOPIndexPath!) -> String! {
        let title = _MenuValue[indexPath.column]
        if(title.OneMenu[indexPath.row].image=="" ||
            title.OneMenu[indexPath.row].image==nil){
            return ""
        }
        return title.OneMenu[indexPath.row].image
    }
    //二级菜单图片(文字左边logo
    func menu(menu: DOPDropDownMenu!, imageNameForItemsInRowAtIndexPath indexPath: DOPIndexPath!) -> String! {
        let title = _MenuValue[indexPath.column]
        if(title.OneMenu[indexPath.row].TowMenu[indexPath.row].image=="" ||
            title.OneMenu[indexPath.row].TowMenu[indexPath.row].image==nil){
            
            return "无数据"
        }
        
        return title.OneMenu[indexPath.row].TowMenu[indexPath.item].image
        
    }
    
    //一级菜单 右边带有数字
    func menu(menu: DOPDropDownMenu!, detailTextForRowAtIndexPath indexPath: DOPIndexPath!) -> String! {
        let value  = _MenuValue[indexPath.column]
        return  value.OneMenu[indexPath.row].TowMenu.count.description  //获取二级列表的总数
    }
    //二级菜单 右边带有数字
    func menu(menu: DOPDropDownMenu!, detailTextForItemsInRowAtIndexPath indexPath: DOPIndexPath!) -> String! {
        
        return "0"
    }
    //选择
    func menu(menu: DOPDropDownMenu!, didSelectRowAtIndexPath indexPath: DOPIndexPath!) {
        
        let title = _MenuValue[indexPath.column]
        if(title.OneMenu[indexPath.row].TowMenu.count==0){
            //一级菜单
//            print(title.OneMenu[indexPath.row].name)
            if(myCallbackValue != nil){
                myCallbackValue!(name: title.OneMenu[indexPath.row].name!,value:title.OneMenu[indexPath.row].value!)
            }
        }
        else{//选中二级
            if(indexPath.item>0){
//                print(title.OneMenu[indexPath.row].TowMenu[indexPath.item].name )
                myCallbackValue!(name: title.OneMenu[indexPath.row].TowMenu[indexPath.item].name!,value: title.OneMenu[indexPath.row].TowMenu[indexPath.item].value!)
            }
        }
    }
    
    

 
}

如需代码,请移动到--->http://www.jianshu.com/p/0f950c180cb8

你可能感兴趣的:(Swift快速集成控件篇(DopDropDownMenu[二级菜单])第三方控件)