效果图:
实现代码:
#define KWidth [UIScreen mainScreen].bounds.size.width
#import "ViewController.h"
#import "UIColor+HCHex.h"
//导入主头文件
#import "MKDropdownMenu.h"
@interface ViewController ()<MKDropdownMenuDataSource,MKDropdownMenuDelegate>
{
NSArray* foodArray;
NSArray* discountsArray;
}
@property (nonatomic, strong)MKDropdownMenu *topDropMenu;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.topDropMenu = [[MKDropdownMenu alloc] initWithFrame:CGRectMake(0, 159, KWidth, 35)];
self.topDropMenu.dataSource = self;
self.topDropMenu.delegate = self;
self.topDropMenu.layer.borderColor = [[UIColor hex_C2C2C2_Color] CGColor];
self.topDropMenu.layer.borderWidth = 0.5;
self.topDropMenu.disclosureIndicatorImage = [UIImage imageNamed:@"voucher_down.png"];
self.topDropMenu.dropdownShowsTopRowSeparator = NO;
self.topDropMenu.dropdownShowsBottomRowSeparator = NO;
self.topDropMenu.dropdownShowsBorder = YES;
self.topDropMenu.backgroundColor = [UIColor clearColor];
self.topDropMenu.componentSeparatorColor = [UIColor hex_929292_Color];
[self.view addSubview:_topDropMenu];
foodArray = @[@"全部美食",@"本帮上海菜",@"日本菜",@"咖啡厅",@"小吃快餐",@"面包甜点",@"火锅",@"西餐",@"自助餐",@"粤菜",@"韩国料理",@"小龙虾",@"轻食",@"东北菜"];
discountsArray = @[@"全部",@"0-30元",@"30-50元",@"50元以上",@"折扣优惠券"];
}
#pragma mark - MKDropdownMenuDataSource
- (NSInteger)numberOfComponentsInDropdownMenu:(MKDropdownMenu *)dropdownMenu {
return 2;
}
- (NSInteger)dropdownMenu:(MKDropdownMenu *)dropdownMenu numberOfRowsInComponent:(NSInteger)component {
switch (component) {
case 0:
return foodArray.count;
case 1:
return discountsArray.count;
default:
return 0;
}
}
#pragma mark - MKDropdownMenuDelegate
- (CGFloat)dropdownMenu:(MKDropdownMenu *)dropdownMenu rowHeightForComponent:(NSInteger)component {
return 35; // default row height
}
- (BOOL)dropdownMenu:(MKDropdownMenu *)dropdownMenu shouldUseFullRowWidthForComponent:(NSInteger)component {
return YES;
}
- (NSAttributedString *)dropdownMenu:(MKDropdownMenu *)dropdownMenu attributedTitleForComponent:(NSInteger)component {
NSString *title;
if (component == 0) {
title = @"美食";
} else {
title = @"优惠";
}
return [[NSAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0],NSForegroundColorAttributeName: [UIColor hex_3A3B3C_Color]}];
}
- (NSAttributedString *)dropdownMenu:(MKDropdownMenu *)dropdownMenu attributedTitleForSelectedComponent:(NSInteger)component {
NSString *title;
if (component == 0) {
title = @"美食";
} else {
title = @"优惠";
}
return [[NSAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0],NSForegroundColorAttributeName: [UIColor redColor]}];
}
- (NSAttributedString *)dropdownMenu:(MKDropdownMenu *)dropdownMenu attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *rowTitle;
if (component == 0) {
rowTitle = foodArray[row];
} else if (component == 1) {
rowTitle = discountsArray[row];
}
return [[NSAttributedString alloc] initWithString:rowTitle attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:10.0],NSForegroundColorAttributeName: [UIColor hex_3A3B3C_Color]}];
}
- (UIColor *)dropdownMenu:(MKDropdownMenu *)dropdownMenu backgroundColorForRow:(NSInteger)row forComponent:(NSInteger)component {
return [UIColor clearColor];
}
- (void)dropdownMenu:(MKDropdownMenu *)dropdownMenu didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[dropdownMenu closeAllComponentsAnimated:YES];
NSLog(@"选择了第 %ld 列的第 %ld 行",(long)component,(long)row);
}
@end