下拉弹出列表选择项效果

右边菜单中的按键,点击弹出一个列表可选择,选择其中一个,响应相应的事件并把文字显示在右边的菜单上;弹出下拉效果使用LMDropdownView插件,可以用POD进行加载pod ‘LMDropdownView’;LMDropdownView是把想要的视图赋给它;

源代码地址:https://github.com/JxbSir/YiYuanYunGou

效果如下:

1:在主页面先定义按键跟绑定视图(没写全的都是属性中定义了比如btnRigth,dropdownView等):

btnRigth = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnRigth addTarget:self action:@selector(btnRightAction) forControlEvents:UIControlEventTouchUpInside];
    if(![OyTool ShardInstance].bIsForReview)
    {
        [self actionCustomNavBtn:btnRigth nrlImage:@"" htlImage:@"" title:@"全部分类▽"];
    }
    else
    {
        [self actionCustomNavBtn:btnRigth nrlImage:@"" htlImage:@"" title:[dicTypeName.allValues objectAtIndex:0]];
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnRigth];

    AllProTypeView* tview = [[AllProTypeView alloc] initWithFrame:self.view.bounds];
    tview.delegate = self;
    //赋于下拉的里效果视图
    dropdownView = [[LMDropdownView alloc] init];
    dropdownView.menuBackgroundColor = [UIColor whiteColor];
    dropdownView.menuContentView = tview;

2:其中对设置按键进行的封装:

- (void)actionCustomNavBtn:(UIButton *)btn nrlImage:(NSString *)nrlImage
                  htlImage:(NSString *)hltImage
                     title:(NSString *)title {
    [btn setImage:[UIImage imageNamed:nrlImage] forState:UIControlStateNormal];
    if (hltImage) {
        [btn setImage:[UIImage imageNamed:hltImage] forState:UIControlStateHighlighted];
    } else {
        [btn setImage:[UIImage imageNamed:nrlImage] forState:UIControlStateNormal];
    }
    if (title) {
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:13];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitle:title forState:UIControlStateHighlighted];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    }
    [btn sizeToFit];
}

3:其中btnRightAction响应事件内容(主要用于显示跟隐藏下拉效果):

- (void)btnRightAction
{
    if ([dropdownView isOpen])
    {
        [dropdownView hide];
    }
    else
    {
        //[tbViewType reloadData];
        [dropdownView showInView:self.view withFrame:CGRectMake(0, 0, mainWidth, self.view.bounds.size.height)];
    }

}

4:AllProTypeView下拉内容的视图代码如下(是一个列表):

.h文件内容

#import <UIKit/UIKit.h>

@protocol AllProTypeViewDelegate
- (void)selectedTypeCode:(int)code;
@end

@interface AllProTypeView : UIView
@property(nonatomic,weak)id<AllProTypeViewDelegate> delegate;
@end

.m文件内容

@interface AllProTypeView ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView     *tbView;

    NSArray         *arrOfType;
    NSArray         *arrOfTypeImage;
    NSInteger       indexType;

    __weak id<AllProTypeViewDelegate> delegate;
}
@end

@implementation AllProTypeView
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        self.backgroundColor = [UIColor redColor];

        tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, mainWidth, frame.size.height) style:UITableViewStyleGrouped];
        tbView.delegate = self;
        tbView.dataSource = self;
        tbView.backgroundColor = [UIColor whiteColor];
        tbView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        tbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self addSubview:tbView];

        if(![OyTool ShardInstance].bIsForReview)
        {
            arrOfType = @[@"全部分类",@"手机数码",@"电脑办公",@"家用电器",@"化妆个护",@"钟表首饰",@"其他商品"];
            arrOfTypeImage = @[@"sort0",@"sort100",@"sort106",@"sort104",@"sort2",@"sort222",@"sort312"];
        }
        else
        {
            arrOfType = @[@"家用电器",@"化妆个护",@"钟表首饰",@"其他商品"];
            arrOfTypeImage = @[@"sort104",@"sort2",@"sort222",@"sort312"];
        }
    }
    return self;
}

#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  arrOfType.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell =  nil;//(UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] init];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"        %@", [arrOfType objectAtIndex:indexPath.row]];
    NSString* name = [arrOfTypeImage objectAtIndex:indexPath.row];

    if(indexPath.row == indexType)
    {
        name = [NSString stringWithFormat:@"%@_checked",name];
        cell.textLabel.textColor = mainColor;

        UIImageView* imgOK = [[UIImageView alloc] initWithFrame:CGRectMake(mainWidth - 32, 14, 20, 16)];
        imgOK.image = [UIImage imageNamed:@"screening_select"];
        [cell addSubview:imgOK];
    }
    else
    {
        name = [NSString stringWithFormat:@"%@_normal",name];
    }
    UIImageView* img = [[UIImageView alloc] initWithFrame:CGRectMake(16, 10, 24, 24)];
    img.image = [UIImage imageNamed:name];
    [cell addSubview:img];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    indexType = indexPath.row;
    [tbView reloadData];

    if(delegate)
    {
        NSString* code = [[arrOfTypeImage objectAtIndex:indexPath.row] stringByReplacingOccurrencesOfString:@"sort" withString:@""];
        [delegate selectedTypeCode:[code intValue]];
    }
}

@end

注意:列表有绑定是否是被选择,它显示的效果是不一样的,在触发行时对标识符进行重新赋值,把通过delegate把它传回主视图控件器里;

5:主控制器里响应上面delegate的内容为:

- (void)selectedTypeCode:(int)code
{
    iCodeType = code;
    [dropdownView hide];

    NSString* key = [NSString stringWithFormat:@"%d",code];
    NSString* name = [dicTypeName objectForKey:key];

    [self actionCustomNavBtn:btnRigth nrlImage:@"" htlImage:@"0" title:name];

   //重新绑定列表显示内容
    __weak typeof (self) wSelf = self;
    curPage = 1;
    [self getData:^{
        __strong typeof (wSelf) sSelf = wSelf;
        sSelf->listNew = nil;
    }];
}

iOS开发——UI篇&下拉弹出列表选择项效果的更多相关文章

  1. iOS开发UI篇—Modal简单介绍

    iOS开发UI篇—Modal简单介绍 一.简单介绍 除了push之外,还有另外一种控制器的切换方式,那就是Modal 任何控制器都能通过Modal的形式展⽰出来 Modal的默认效果:新控制器从屏幕的 ...

  2. iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建

    iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...

  3. iOS开发UI篇—Date Picker和UITool Bar控件简单介绍

    iOS开发UI篇—Date Picker和UITool Bar控件简单介绍 一.Date Picker控件 1.简单介绍: Date Picker显示时间的控件 有默认宽高,不用设置数据源和代理 如何 ...

  4. iOS开发UI篇—CAlayer(创建图层)

    iOS开发UI篇—CAlayer(创建图层) 一.添加一个图层 添加图层的步骤: 1.创建layer 2.设置layer的属性(设置了颜色,bounds才能显示出来) 3.将layer添加到界面上(控 ...

  5. iOS开发UI篇—UITableviewcell的性能优化和缓存机制

    iOS开发UI篇—UITableviewcell的性能问题 一.UITableviewcell的一些介绍 UITableView的每一行都是一个UITableViewCell,通过dataSource ...

  6. iOS开发UI篇—实现UItableview控件数据刷新

    iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...

  7. iOS开发UI篇—UIWindow简单介绍

    iOS开发UI篇—UIWindow简单介绍 一.简单介绍 UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow iOS程序启动完毕后,创建的第一个视图控件就是UIWi ...

  8. iOS开发UI篇—无限轮播(功能完善)

    iOS开发UI篇—无限轮播(功能完善) 一.自动滚动 添加并设置一个定时器,每个2.0秒,就跳转到下一条. 获取当前正在展示的位置. [self addNSTimer]; } -(void)addNS ...

  9. iOS开发UI篇—popoverController使用注意

    iOS开发UI篇—popoverController使用注意 一.设置尺寸 提示:不建议,像下面这样吧popover的宽度和高度写死. //1.新建一个内容控制器 YYMenuViewControll ...

随机推荐

  1. [Everyday Mathematics]20150205

    设 $\phi:[k_0,\infty)\to[0,\infty)$ 是有界递减函数, 并且 $$\bex \phi(k)\leq \sex{\frac{A}{h-k}}^\al\phi(h)^\be ...

  2. 读pomelo的教程-1

    pomelo教程的例子是一个聊天室,包括一个webserver客户端,和一个gameserver的pomelo服务器.这个例子挺好,一个聊天系统逻辑简单,还包括了用户管理,客户端request,服务器 ...

  3. springMVC(注解版笔记)

    springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:compon ...

  4. BBED的安装

    BBED是Block Browser EDitor的缩写,只有linux/unix版本,没有windows版本. 11g中默认是不带bbed的,如果要使用,可以在10g中拷贝过来,然后再进行编译使用. ...

  5. sensor BMA250源代码执行分析

    重力传感器是根据压电效应的原理来工作的.   所谓的压电效应就是 “对于不存在对称中心的异极晶体加在晶体上的外力除了使晶体发生形变以外,还将改变晶体的极化状态,在晶体内部建立电场,这种由于机械力作用使 ...

  6. Language Basics:语言基础

    Java包含多种变量类型:Instance Variables (Non-Static Fields)(实例变量):是每个对象特有的,可以用来区分各个实例Class Variables (Static ...

  7. 《Genesis-3D开源游戏引擎-FQA常见问题解答》2014年01月10号版本

    1.Genesis-3D开源游戏引擎主要面向哪些用户人群?有限制吗? 1.我们的引擎没有限制,只要您想了解和使用我们的引擎,就可以加入Genesis-3D的大家庭.2.我们的主要用户群是各个相关的企业 ...

  8. 怎么利用SQL语句查询数据库中具体某个字段的重复行

    select * from [tablename] group by SeriNohaving count(SeriNo)<>1

  9. <Chapter 2>2-2-2.开发Python应用(Developing a Python App)

    对App Engine来讲最简单的Python应用是一个有两个文件的简单目录:一个称为app.yaml的配置文件,一个用于请求处理器的Python代码文件.包含app.yaml文件的这个目录就是这个应 ...

  10. HDU5734:Acperience(方差)

    题意: 给出n个数xi,确定一个值α,使得Σ(xi-α)^2的值最小. 分析: 可以猜想是方差,不懂得可以去方差了解一下. 那么α即为∑(xi)/n,然后要注意的是转化为分数,首先我们不能用小数转分数 ...