美文网首页
下拉列表/DOPDropDownMenu使用详解

下拉列表/DOPDropDownMenu使用详解

作者: xxxixxxx | 来源:发表于2016-08-13 09:28 被阅读1924次

github原址

呆! 刚发现github写的很详细。。。

效果图
  1. 导入头文件
    #import "DOPDropDownMenu.h"
  1. 设置代理
    <DOPDropDownMenuDataSource,DOPDropDownMenuDelegate>
  2. 创建数组 与 下拉菜单
@property (nonatomic, strong) NSArray *classifys;
///美食列表数组
@property (nonatomic, strong) NSArray *cates;
///电影列表数组
@property (nonatomic, strong) NSArray *movices;
///酒店列表数组
@property (nonatomic, strong) NSArray *hostels;
///区域列表数组
@property (nonatomic, strong) NSArray *areas;
///排序列表数组
@property (nonatomic, strong) NSArray *sorts;
///下拉菜单
@property (nonatomic, weak) DOPDropDownMenu *menu;
  1. 数据 与 菜单的初始化创建
// 数据
    self.classifys = @[@"美食",@"今日新单",@"电影",@"酒店"];
    self.cates = @[@"自助餐",@"快餐",@"火锅",@"日韩料理",@"西餐",@"烧烤小吃"];
    self.movices = @[@"内地剧",@"港台剧",@"英美剧"];
    self.hostels = @[@"经济酒店",@"商务酒店",@"连锁酒店",@"度假酒店",@"公寓酒店"];
    self.areas = @[@"全城",@"芙蓉区",@"雨花区",@"天心区",@"开福区",@"岳麓区"];
    self.sorts = @[@"默认排序",@"离我最近",@"好评优先",@"人气优先",@"最新发布"];
    // 添加下拉菜单
    DOPDropDownMenu *menu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, 64) andHeight:44];
    menu.delegate = self;
    menu.dataSource = self;
    [self.view addSubview:menu];
    _menu = menu;
    // 创建menu 第一次显示 不会调用点击代理,可以用这个手动调用
    [menu selectDefalutIndexPath];
  1. 详细设置
 #pragma mark - 设置列数
 - (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu
{
    return 3;
}
 #pragma mark 设置每一列有几项
 - (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column
{
    if (column == 0) {
        return self.classifys.count;
    }else if (column == 1){
        return self.areas.count;
    }else {
        return self.sorts.count;
    }
}
 #pragma mark 设置每一列中每一项标题
 - (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath
{
    if (indexPath.column == 0) {
        return self.classifys[indexPath.row];
    } else if (indexPath.column == 1){
        return self.areas[indexPath.row];
    } else {
        return self.sorts[indexPath.row];
    }
}
 #pragma mark 设置每一列中每一项的图片   如:美食
 - (NSString *)menu:(DOPDropDownMenu *)menu imageNameForRowAtIndexPath:(DOPIndexPath *)indexPath
{
    //这里设置的是 第一列和第二列 图片一样
    if (indexPath.column == 0 || indexPath.column == 1) {
        return [NSString stringWithFormat:@"ic_filter_category_%ld",indexPath.row];
    }
    return nil;
}
 #pragma mark 设置底层分类图片  如:美食中每一项的图片
 - (NSString *)menu:(DOPDropDownMenu *)menu imageNameForItemsInRowAtIndexPath:(DOPIndexPath *)indexPath
{
    //这里设置的是 第一列每一项中 的图片
    if (indexPath.column == 0 && indexPath.item >= 0) {
        return [NSString stringWithFormat:@"ic_filter_category_%ld",indexPath.item];
    }
    return nil;
}
 #pragma mark 设置每一列中每一项的个数
 - (NSString *)menu:(DOPDropDownMenu *)menu detailTextForRowAtIndexPath:(DOPIndexPath *)indexPath
{
    //这里设置的是前两列中 每一项的个数
    if (indexPath.column < 2) {
        return [@(arc4random()%1000) stringValue];
    }
    return nil;
}
 #pragma mark 设置每一列每一项中的个数
 - (NSString *)menu:(DOPDropDownMenu *)menu detailTextForItemsInRowAtIndexPath:(DOPIndexPath *)indexPath
{
    //这里 因为只有第一列 有三级菜单  所以不判断是那一列 如果要判断参考上边
    return [@(arc4random()%1000) stringValue];
}
  #pragma mark 设置每一列中每一项有多少项
 - (NSInteger)menu:(DOPDropDownMenu *)menu numberOfItemsInRow:(NSInteger)row column:(NSInteger)column
{
    //这里只设置了第一列
    if (column == 0) {
        if (row == 0) {
           return self.cates.count;
        } else if (row == 1){
            return 5;
        } else if (row == 2){
            return self.movices.count;
        } else if (row == 3){
            return self.hostels.count;
        }
    }  
   return 0;
}
 #pragma mark 设置每一列中的每一项中的所有标题
 - (NSString *)menu:(DOPDropDownMenu *)menu titleForItemsInRowAtIndexPath:(DOPIndexPath *)indexPath
{
    //这里只设置了第一列
    if (indexPath.column == 0) {
        if (indexPath.row == 0) {
            return self.cates[indexPath.item];
        } else if (indexPath.row == 1) {
            return @"哈哈哈";
        } else if (indexPath.row == 2){
            return self.movices[indexPath.item];
        } else if (indexPath.row == 3){
            return self.hostels[indexPath.item];
        }
    }   
    return nil;
}
 #pragma mark - 接收点击事件
 - (void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath
{
    if (indexPath.item >= 0) {
         NSLog(@"点击了 %ld - %ld - %ld 项目",indexPath.column,indexPath.row,indexPath.item);
    }else {
         NSLog(@"点击了 %ld - %ld 项目",indexPath.column,indexPath.row);
    }
}
  1. 可选项
//TODO: 设置一个按钮 选中某一列某一项
 - (IBAction)selectIndexPathAction:(id)sender {
    [_menu selectIndexPath:[DOPIndexPath indexPathWithCol:0 row:2 item:2]];
}

相关文章

网友评论

      本文标题:下拉列表/DOPDropDownMenu使用详解

      本文链接:https://www.haomeiwen.com/subject/hrbesttx.html