美文网首页iOS学习笔记iOS开发技术分享iOS进阶指南
关于我个人总结ios的下拉菜单使用方法

关于我个人总结ios的下拉菜单使用方法

作者: Wo的小名叫单纯 | 来源:发表于2016-06-07 14:09 被阅读2556次

    什么不多说,见代码

    首先要封装一个类 NIDropDown
    这个类继承与UIView;


    屏幕快照 2016-06-07 下午1.57.34.png

    关于h文件里面的东西

    import <UIKit/UIKit.h>

    @class NIDropDown;
    @protocol NIDropDownDelegate

    • (void) niDropDownDelegateMethod: (NIDropDown *) sender;
      @end

    @interface NIDropDown : UIView <UITableViewDelegate, UITableViewDataSource>

    @property (nonatomic, retain) id <NIDropDownDelegate> delegate;

    -(void)hideDropDown:(UIButton *)b;

    • (id)showDropDown:(UIButton *)b:(CGFloat *)height:(NSArray *)arr;

    @end

    关于m文件里面的东西

    import "NIDropDown.h"

    import "QuartzCore/QuartzCore.h"

    @interface NIDropDown ()
    @property(nonatomic,strong)UITableView *table;
    @property(nonatomic,strong)UIButton *btnSender;
    @property(nonatomic,retain)NSArray *list;
    @end

    @implementation NIDropDown

    @synthesize table;
    @synthesize btnSender;
    @synthesize list;
    @synthesize delegate;

    -(id)showDropDown:(UIButton *)b :(CGFloat *)height :(NSArray *)arr
    {
    btnSender = b;
    if(self){
    CGRect btn = b.frame;
    //self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
    [self initWithFrame:CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0)];
    self.list = [NSArray arrayWithArray:arr];
    self.layer.masksToBounds = NO;
    self.layer.cornerRadius = 8;
    self.layer.shadowOffset = CGSizeMake(-5, 5);
    self.layer.shadowRadius = 5;
    self.layer.shadowOpacity = 0.5;

        table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btn.size.width, 0)];
        table.delegate = self;
        table.dataSource = self;
        table.layer.cornerRadius = 5;
        table.backgroundColor = [UIColor redColor];
        table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        table.separatorColor = [UIColor grayColor];
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        self.frame = CGRectMake(btn.origin.x, btn.origin.y + btn.size.height, btn.size.width, *height);
        
        table.frame = CGRectMake(0, 0, btn.size.width, *height);
        [UIView commitAnimations];
        
        [b.superview addSubview:self];
        [self addSubview:table];
    }
    return self;
    

    }

    -(void)hideDropDown:(UIButton *)b
    {
    CGRect btn = b.frame;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    self.frame = CGRectMake(btn.origin.x, btn.origin.y + btn.size.height, btn.size.width, 0);
    table.frame = CGRectMake(0, 0, btn.size.width, 0);
    [UIView commitAnimations];
    }

    -(NSInteger)numberOfSectionsInTableView:(nonnull UITableView *)tableView
    {
    return 1;
    }

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

    • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      return [self.list count];
      }

    • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      static NSString *CellIdentifier = @"Cell";

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if(cell == nil){
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      cell.textLabel.font = [UIFont systemFontOfSize:15];
      cell.textLabel.textAlignment = UITextAlignmentCenter;
      }
      cell.textLabel.text = [list objectAtIndex:indexPath.row];
      cell.textLabel.textColor = [UIColor redColor];

      UIView *v = [[UIView alloc] init];
      v.backgroundColor = [UIColor grayColor];
      cell.selectedBackgroundView = v;

      return cell;
      }

    -(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
    {
    [self hideDropDown:btnSender];
    UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
    [btnSender setTitle:c.textLabel.text forState:(UIControlStateNormal)];
    [self myDelegate];
    }

    -(void)myDelegate
    {
    [self.delegate niDropDownDelegateMethod:self];

    }

    写完这个类以后,回到自己的viewController里面
    h文件
    引入NIDropDown这个类

    import <UIKit/UIKit.h>

    import "NIDropDown.h"

    @interface ViewController : UIViewController<NIDropDownDelegate>
    {
    NIDropDown *dropDown;
    }

    @property(nonatomic,strong)UIButton *button;

    -(void)rel;

    @end

    m文件
    button点击方法:
    -(void)buttonAction:(UIButton *)sender
    {
    NSArray * arr = [[NSArray alloc] init];
    arr = [NSArray arrayWithObjects:@"Hello 0", @"Hello 1", @"Hello 2", @"Hello 3", @"Hello 4", @"Hello 5", @"Hello 6", @"Hello 7", @"Hello 8", @"Hello 9",nil];
    if(dropDown == nil) {
    CGFloat f = 200;
    dropDown = [[NIDropDown alloc]showDropDown:sender :&f :arr];
    dropDown.delegate = self;
    }
    else {
    [dropDown hideDropDown:sender];
    [self rel];
    }

    }

    -(void)niDropDownDelegateMethod:(NIDropDown *)sender
    {
    [self rel];
    }
    -(void)rel{
    dropDown = nil;
    }

    相关文章

      网友评论

        本文标题:关于我个人总结ios的下拉菜单使用方法

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