關(guān)于我個(gè)人總結(jié)ios的下拉菜單使用方法

什么不多說,見代碼

首先要封裝一個(gè)類 NIDropDown
這個(gè)類繼承與UIView;


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

關(guān)于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

關(guān)于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];

}

寫完這個(gè)類以后,回到自己的viewController里面
h文件
引入NIDropDown這個(gè)類

import <UIKit/UIKit.h>

import "NIDropDown.h"

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

@property(nonatomic,strong)UIButton *button;

-(void)rel;

@end

m文件
button點(diǎn)擊方法:
-(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;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末煮寡,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌池磁,老刑警劉巖管毙,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件兴使,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡骑丸,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門妒貌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來通危,“玉大人,你說我怎么就攤上這事灌曙【盏” “怎么了?”我有些...
    開封第一講書人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵在刺,是天一觀的道長(zhǎng)逆害。 經(jīng)常有香客問我,道長(zhǎng)蚣驼,這世上最難降的妖魔是什么魄幕? 我笑而不...
    開封第一講書人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮颖杏,結(jié)果婚禮上纯陨,老公的妹妹穿的比我還像新娘。我一直安慰自己留储,他們只是感情好翼抠,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著获讳,像睡著了一般机久。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上赔嚎,一...
    開封第一講書人閱讀 51,274評(píng)論 1 300
  • 那天膘盖,我揣著相機(jī)與錄音,去河邊找鬼尤误。 笑死侠畔,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的损晤。 我是一名探鬼主播软棺,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼尤勋!你這毒婦竟也來了喘落?” 一聲冷哼從身側(cè)響起茵宪,我...
    開封第一講書人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎瘦棋,沒想到半個(gè)月后稀火,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡赌朋,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年凰狞,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片沛慢。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡赡若,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出团甲,到底是詐尸還是另有隱情逾冬,我是刑警寧澤,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布躺苦,位于F島的核電站粉渠,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏圾另。R本人自食惡果不足惜霸株,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望集乔。 院中可真熱鬧去件,春花似錦、人聲如沸扰路。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)汗唱。三九已至宫莱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間哩罪,已是汗流浹背授霸。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留际插,地道東北人碘耳。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像框弛,于是被迫代替她去往敵國(guó)和親辛辨。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容