iOS随笔小记--自定义下单菜单
2017-06-23 本文已影响27人
七一小月
效果图:
![](https://img.haomeiwen.com/i2849255/3334b914464a820f.png)
![](https://img.haomeiwen.com/i2849255/5cb927a33ec63ab3.png)
.h文件里面要实现的协议的方法
在YDDropdownMenuDelegate里面要实现的协议方法有以下几种:
/**
* net代理方法返回 菜单下标:index 菜单标题:MenuTitle
*/
- (void)netClickMenuIndex:(NSInteger)menuIndex menuTitle:(NSString *)menuTitle;
/**
* 代理方法返回 菜单下标:index 一级菜单index:firstIndex 一级菜单名
*/
- (void)menuCellDidSelected:(NSInteger)MenuTitleIndex firstIndex:(NSInteger)firstIndex andfirstIndexName:(NSString *)firstIndexName;
/**
* 代理方法返回 菜单下标:index 二级菜单index:firstIndex 二级菜单名
*/
- (void)menuCellDidSelected:(NSInteger)MenuTitleIndex secondIndex:(NSInteger)secondIndex andsecondIndexName:(NSString *)secondIndexName;
/**
*代理方法返回 菜单下标:index 三级菜单index:firstIndex 三级菜单名
*/
- (void)menuCellDidSelected:(NSInteger)MenuTitleIndex thirdIndex:(NSInteger)thirdIndex andthirdIndexName:(NSString *)thirdIndexName;
/**
* 代理方法返回,选中的category筛选产品
*/
-(void)menuCellDidSelected:(NSInteger)MenuTitleIndex andCategoryName:(NSString *)categoryName;
/**
* 代理方法返回,将本身视图显示在最上层
*/
-(void)bringselfViewToFront;
/**
* 代理方法返回,将本身视图显示在最collectionview下层
*/
-(void)bringselfViewToBackCollection;
定义的一些常用属性(可在调用时设置,如若不设置,则会调用默认的值):
/** 遮盖层颜色 */
@property (nonatomic, strong) UIColor * CarverViewColor;
/** 自定义tag值防止和页面其他tag有冲突 */
@property (nonatomic, assign) NSInteger menuButtonTag;
/** 遮盖的动画时间 */
@property (nonatomic, assign) CGFloat caverAnimationTime;
/** 缩进的动画时间 */
@property (nonatomic, assign) CGFloat hideAnimationTime;
/** 菜单title的字体大小 */
@property (nonatomic, assign) CGFloat menuTitleFont;
/** 下拉菜单的的字体大小 */
@property (nonatomic, assign) CGFloat tableTitleFont;
/** 菜单的的高度 */
@property (nonatomic, assign) CGFloat menuHeight;
/** 下拉菜单cell的高度 */
@property (nonatomic, assign) CGFloat cellHeight;
/** 下拉的Tableview最大高度(超出高度可以滑动显示) */
@property (nonatomic, assign) CGFloat tableViewMaxHeight;
/** 未选中字体的颜色 */
@property (nonatomic, strong) UIColor * unSelectedColor;
/** 选中字体的颜色 */
@property (nonatomic, strong) UIColor * selectedColor;
/** 选中背景图片 */
@property (nonatomic, strong) UIImage * selectedImage;
/** 设置代理 */
@property (nonatomic,weak) id <YDDropdownMenuDelegate> delegate;
@property (nonatomic,strong) UITableView * tableFirst;
@property (nonatomic,strong) UITableView * tableSecond;
@property (nonatomic,strong) UITableView * tableThird;
在初始化各个下拉的tableview时调用以下的方法进行创建:
#pragma mark -- net一级一级点击网络获取数据创建
/** net创建下拉菜单 */
- (void)netCreateMenuTitleArray:(NSArray *)menuTitleArray;
/** net导入一级菜单数据 */
- (void)netLoadFirstArray:(NSArray *)firstArray andMenuIndex: (NSInteger)menuIndex andArrow:(BOOL)arrow;
/** net导入二级菜单数据 */
- (void)netLoadSecondArray:(NSArray *)SecondArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow;
/** net导入三级菜单数据 */
- (void)netLoadThirdArray:(NSArray *)ThirdArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow;
-(void)reloadTableView:(UITableView *)tableView;
.m文件里面要实现的方法
宏定义的一些默认值(在用户初始化不设置时调用):
#define cell_h 33
#define carverAnimationDefalutTime 0.15
#define hideAnimationDefalutTime 0.15
#define tableViewMaxHeightDefalut 198
#define menuButtonDefalutTag 700
#define triangleImageDefalutTag 100
#define menuTitleDefalutFont [UIFont systemFontOfSize:13]
#define TableTitleDefalutFont [UIFont systemFontOfSize:11]
#define SeparatorColor YDColor(249, 249, 249)
#define Drop_triangle @"Drop_triangle"
#define Pullup_triangle @"Pullup_triangle"
定义的一些属性:
@interface YDDropdownMenu ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIImageView * triangleImageV; //三角形图标
@property (nonatomic, strong) UIView * buttonBackView;
@property (nonatomic, strong) UIButton * typeBtn ;
@property (nonatomic, strong) NSMutableArray * dataSourceFirst;
@property (nonatomic, strong) NSMutableArray * dataSourceSecond;
@property (nonatomic, strong) NSMutableArray * dataSourceThird;
@property (nonatomic, assign) BOOL firstTableViewShow;
@property (nonatomic, assign) BOOL secondTableViewShow;
@property (nonatomic, assign) BOOL thirdTableViewShow;
@property (nonatomic, assign) NSInteger lastSelectedButtonIndex;
@property (nonatomic, assign) NSInteger lastSelectedCellIndex;
@property (nonatomic, assign) NSInteger lastSecondCellIndex;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, assign) CGFloat tableViewWith;
@property (nonatomic, assign) CGFloat buttonBackWithSum;
@property (nonatomic, assign) CGFloat menuBaseHeight;
@property (nonatomic, assign) BOOL isNet;
@property (nonatomic, assign) BOOL arrow;
@property (nonatomic, assign) BOOL hasThirdTableView;
@end
设置数组懒加载:
-(NSMutableArray *)dataSourceFirst{
if (!_dataSourceFirst) {
_dataSourceFirst = [[NSMutableArray alloc] init];
}
return _dataSourceFirst;
}
-(NSMutableArray *)dataSourceThird{
if (!_dataSourceThird) {
_dataSourceThird = [[NSMutableArray alloc] init];
}
return _dataSourceThird;
}
-(NSMutableArray *)dataSourceSecond{
if (!_dataSourceSecond) {
_dataSourceSecond = [[NSMutableArray alloc] init];
}
return _dataSourceSecond;
}
添加removeDropdownMenuNotifacation通知,在外部发送通知隐藏下拉菜单时接收通知,对tableview菜单进行隐藏:
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeDropdownMenu:) name:removeDropdownMenuNotifacation object:nil];
}
return self;
}
创建下拉主菜单(循环创建需要的button个数):
#pragma mark -- net一级一级点击网络获取数据创建
- (void)netCreateMenuTitleArray:(NSArray *)menuTitleArray{
self.isNet = YES;
self.menuBaseHeight = 18;
self.tableViewMaxHeight = self.cellHeight * 6;
[self createMenuViewWithData:menuTitleArray];
[self createTableViewFirst];
[self createTableViewSecond];
[self createTableViewThird];
}
创建每个button下可能需要的tableview菜单:
//创建第一级目录的tableview
- (void)netLoadFirstArray:(NSArray *)firstArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow{
self.arrow = arrow;
[self createWithFirstData:firstArray];
[self netShowFirstTableByMenuIndex:menuIndex];
}
//创建第二级目录的tableview
- (void)netLoadSecondArray:(NSArray *)SecondArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow{
self.arrow = arrow;
if ( SecondArray.count > 0) {
[self.dataSourceSecond removeAllObjects];
[self.dataSourceSecond addObjectsFromArray:SecondArray];
}
if( arrow == YES ){
self.hasThirdTableView = YES;
}
[self showSecondTabelView:self.secondTableViewShow MenuIndex:menuIndex];
}
//创建第三级目录的tableview
-(void)netLoadThirdArray:(NSArray *)ThirdArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow{
self.arrow = arrow;
if (ThirdArray.count > 0) {
[self.dataSourceThird removeAllObjects];
[self.dataSourceThird addObjectsFromArray:ThirdArray];
[self showThirdTabelView:self.thirdTableViewShow MenuIndex:menuIndex];
[self.tableThird reloadData];
}
else{
UIButton *btn = (UIButton *)[self viewWithTag:self.lastSelectedButtonIndex];
[btn setTitle:_dataSourceSecond[self.lastSelectedCellIndex] forState:UIControlStateNormal];
btn.selected = YES;
[self remover];
}
}
当需要显示某个tableview时进行调用:
//显示第一级目录的tableview
- (void)netShowFirstTableByMenuIndex:(NSInteger)menuIndex{
// NSInteger tempTag = self.menuButtonTag ? self.menuButtonTag :menuButtonDefalutTag;
// NSInteger index = self.lastSelectedButtonIndex;
CGFloat TableViewW = self.cellHeight*self.dataSourceFirst.count;
CGFloat MaxHeigth = self.tableViewMaxHeight ? self.tableViewMaxHeight : tableViewMaxHeightDefalut;
//判断是否需要滑动
if (TableViewW > MaxHeigth) {
self.tableFirst.scrollEnabled = YES;
TableViewW = MaxHeigth;
}else{
self.tableFirst.scrollEnabled = NO;
}
if (self.firstTableViewShow == NO) {
self.tableFirst.frame = CGRectMake(self.tableViewWith*(menuIndex - 700), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
[UIView animateWithDuration:self.caverAnimationTime animations:^{
self.tableFirst.frame = CGRectMake(self.tableViewWith*(menuIndex - 700), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, TableViewW);
}completion:^(BOOL finished) {
}];
}else{
self.firstTableViewShow = NO;
self.tableFirst.frame = CGRectMake(0, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, TableViewW);
[UIView animateWithDuration:self.hideAnimationTime animations:^{
self.tableFirst.frame = CGRectMake(0, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
}];
}
}
//显示第二级目录的tableview
- (void)showSecondTabelView:(BOOL)secondTableViewShow MenuIndex:(NSInteger)menuIndex{
CGFloat TableViewW = self.cellHeight*self.dataSourceSecond.count;
CGFloat MaxHeigth = self.tableViewMaxHeight ? self.tableViewMaxHeight : tableViewMaxHeightDefalut;
if (TableViewW > MaxHeigth) {
self.tableSecond.scrollEnabled = YES;
TableViewW = MaxHeigth;
}else{
self.tableSecond.scrollEnabled = NO;
}
if (self.secondTableViewShow == YES) {
self.tableSecond.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +1), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
[UIView animateWithDuration:self.caverAnimationTime animations:^{
self.tableSecond.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +1), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, TableViewW);
}];
}else{
self.secondTableViewShow = YES;
self.tableSecond.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +1), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
[UIView animateWithDuration:self.caverAnimationTime animations:^{
self.tableSecond.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +1), CGRectGetMaxY(self.buttonBackView.frame),self.tableViewWith, TableViewW);
}];
}
[self.tableSecond reloadData];
}
//显示第三级目录的tableview
- (void)showThirdTabelView:(BOOL)thirdTableViewShow MenuIndex: (NSInteger)menuIndex{
CGFloat TableViewW = self.cellHeight*self.dataSourceThird.count;
CGFloat MaxHeigth = self.tableViewMaxHeight ? self.tableViewMaxHeight : tableViewMaxHeightDefalut;
if (TableViewW > MaxHeigth) {
self.tableThird.scrollEnabled = YES;
TableViewW = MaxHeigth;
}else{
self.tableThird.scrollEnabled = NO;
}
if (self.thirdTableViewShow == YES) {
self.tableThird.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +2), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
[UIView animateWithDuration:self.caverAnimationTime animations:^{
self.tableThird.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +2), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, TableViewW);
}];
}else{
self.thirdTableViewShow = YES;
self.tableThird.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +2), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
[UIView animateWithDuration:self.caverAnimationTime animations:^{
self.tableThird.frame = CGRectMake(self.tableViewWith*(menuIndex - 700 +2), CGRectGetMaxY(self.buttonBackView.frame),self.tableViewWith, TableViewW);
}];
}
}
当需要隐藏tableview时进行调用:
- (void)hideCarverView{
self.firstTableViewShow = NO;
self.secondTableViewShow = NO;
self.thirdTableViewShow = NO;
if (_delegate && [_delegate respondsToSelector:@selector(bringselfViewToBackCollection)]) {
[_delegate bringselfViewToBackCollection];
}
[UIView animateWithDuration:self.hideAnimationTime animations:^{
self.tableFirst.frame = CGRectMake(self.tableViewWith*(_lastSelectedButtonIndex-700), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
self.tableSecond.frame = CGRectMake(self.tableViewWith, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
self.tableThird.frame = CGRectMake(self.tableViewWith * 2, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
self.backgroundColor = [UIColor clearColor];
}];
NSInteger tempTag = self.menuButtonTag ? self.menuButtonTag :menuButtonDefalutTag;
for (int i =0; i < 3;i++) {
UIButton * button = (id)[self viewWithTag:tempTag + i];
button.selected = YES;
[button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
}
}
给tableview赋值:
#pragma mark -- 给tableview赋值
- (void)createWithFirstData:(NSArray *)dataFirst{
self.dataSourceFirst = [NSMutableArray arrayWithArray:dataFirst];
[self.tableFirst reloadData];
}
- (void)createWithSecondData:(NSArray *)dataSecond{
self.dataSourceSecond = [NSMutableArray arrayWithArray:dataSecond];
[self.tableSecond reloadData];
}
- (void)createWithThirdData:(NSArray *)dataThird{
self.dataSourceThird = [NSMutableArray arrayWithArray:dataThird];
[self.tableThird reloadData];
}
创建分类按钮:
#pragma mark -- 创建分类按钮
-(void)createMenuViewWithData:(NSArray *)menuTitleArray{
self.buttonBackWithSum = 240.0;
self.tableViewWith = self.buttonBackWithSum*0.3333;
self.backgroundColor = [UIColor clearColor];
self.caverAnimationTime = self.caverAnimationTime ? self.caverAnimationTime : carverAnimationDefalutTime;
self.hideAnimationTime = self.hideAnimationTime ? self.hideAnimationTime : hideAnimationDefalutTime;
self.cellHeight = self.cellHeight ? self.cellHeight : cell_h;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(remover)];
[self addGestureRecognizer:tap];
tap.delegate = self;
self.buttonBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 33)];
self.buttonBackView.userInteractionEnabled = YES;
self.buttonBackView.backgroundColor = [UIColor whiteColor];
[self addSubview:self.buttonBackView];
UIImageView * backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 35)];
backImage.image = [UIImage imageNamed:@"typeSelected_backImage"];
[self.buttonBackView addSubview: backImage];
NSInteger num = menuTitleArray.count;
// CGFloat btnW = (self.buttonBackWithSum-num+1)/num;
for (int i = 0; i < num; i++) {
//(btnW+1)*i + 16
_typeBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.tableViewWith *i+ 16, 8, 55, self.menuBaseHeight)];
NSInteger tempTag = self.menuButtonTag ? self.menuButtonTag :menuButtonDefalutTag;
_typeBtn.tag = tempTag+i;
_typeBtn.layer.cornerRadius = 5;
_typeBtn.selected = YES;
_typeBtn.titleLabel.font = self.menuTitleFont ? [UIFont systemFontOfSize:self.menuTitleFont] : menuTitleDefalutFont;
_typeBtn.backgroundColor = [UIColor whiteColor];
_typeBtn.adjustsImageWhenHighlighted = NO;
[_typeBtn setTitle: menuTitleArray[i] forState:UIControlStateNormal];
UIColor * titleColor = self.unSelectedColor?self.unSelectedColor : [UIColor lightGrayColor];
[_typeBtn setTitleColor:titleColor forState:UIControlStateNormal];
_typeBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
[_typeBtn addTarget:self action:@selector(showFirstTableViewByButton:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonBackView addSubview:_typeBtn];
//三角形
_triangleImageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.tableViewWith * (i+1) - 17, CGRectGetMidY(_typeBtn.frame), 4.5, 2.5)];
_triangleImageV.image = [UIImage imageNamed:Drop_triangle];
_triangleImageV.tag = triangleImageDefalutTag + i;
[self.buttonBackView addSubview:_triangleImageV];
}
}
//三角形颜色
-(void)DefalutTriangleImage{
for (int i = 0; i < 3; i++) {
UIImageView * triangleImageV = (id)[self viewWithTag:triangleImageDefalutTag + i];
triangleImageV.image = [UIImage imageNamed:Drop_triangle];
}
}
通过点击某个button来显示第一个tableview(第一次点击menu上面的button):
- (void)showFirstTableViewByButton:(UIButton *)btn{
_lastSelectedButtonIndex = btn.tag;
[self hideCarverView];
[self DefalutTriangleImage];
if (btn.selected == YES) {
NSInteger tempTag = self.menuButtonTag ? self.menuButtonTag :menuButtonDefalutTag;
UIImageView * triangleImageV = (id)[self viewWithTag:triangleImageDefalutTag + (_lastSelectedButtonIndex -tempTag)];
triangleImageV.image = [UIImage imageNamed:Pullup_triangle];
if (_delegate && [_delegate respondsToSelector:@selector(bringselfViewToFront)]) {
[_delegate bringselfViewToFront];
}
[UIView animateWithDuration:0.5 animations:^{
self.backgroundColor = YDColorAlp(12, 13, 28, 0.2);
}];
btn.selected = NO;
if (_delegate && [_delegate respondsToSelector:@selector(netClickMenuIndex:menuTitle:)]) {
[_delegate netClickMenuIndex:btn.tag menuTitle:btn.titleLabel.text];
}
for (UIButton * button in btn.superview.subviews) {
if (button.tag != _lastSelectedButtonIndex && [button isKindOfClass: [UIButton class]]) {
button.selected = YES;
[button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
}
}
[btn setBackgroundImage:self.selectedImage forState:UIControlStateNormal];
}else{
if (_delegate && [_delegate respondsToSelector:@selector(bringselfViewToBackCollection)]) {
[_delegate bringselfViewToBackCollection];
}
self.firstTableViewShow = NO;
btn.selected = YES;
[UIView animateWithDuration:self.hideAnimationTime animations:^{
self.backgroundColor = [UIColor clearColor];
self.tableFirst.frame = CGRectMake(self.tableViewWith*(_lastSelectedButtonIndex-700), CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
self.tableSecond.frame = CGRectMake(self.tableViewWith, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
self.tableThird.frame = CGRectMake(self.tableViewWith * 2, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
}];
}
}
真正实现创建tableview的方法(上面只是在调用此方法):
#pragma mark -- 创建tableview
//创建第一级目录的tableview的实现方法
- (void)createTableViewFirst{
self.tableFirst = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.buttonBackView.frame),self.buttonBackWithSum, 0) style:UITableViewStylePlain];
self.tableFirst.scrollEnabled = NO;
self.tableFirst.delegate = self;
self.tableFirst.dataSource = self;
[self.tableFirst setSeparatorColor:SeparatorColor];
self.firstTableViewShow = NO;
[self.tableFirst registerNib:[UINib nibWithNibName:product_TypeIdentifier bundle:nil] forCellReuseIdentifier:product_TypeIdentifier];
[self insertSubview:self.tableFirst belowSubview:self.buttonBackView];
}
//创建第二级目录的tableview的实现方法
- (void)createTableViewSecond{
self.tableSecond = [[UITableView alloc]initWithFrame:CGRectMake(self.tableViewWith, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0) style:UITableViewStylePlain];
self.tableSecond.scrollEnabled = NO;
self.tableSecond.delegate = self;
self.tableSecond.dataSource = self;
self.tableSecond.autoresizesSubviews = NO;
[self.tableSecond setSeparatorColor:SeparatorColor];
self.secondTableViewShow = NO;
[self.tableSecond registerNib:[UINib nibWithNibName:product_TypeIdentifier bundle:nil] forCellReuseIdentifier:product_TypeIdentifier];
[self insertSubview:self.tableSecond belowSubview:self.buttonBackView];
}
//创建第三级目录的tableview的实现方法
- (void)createTableViewThird{
self.hasThirdTableView = NO;
self.tableThird = [[UITableView alloc]initWithFrame:CGRectMake(self.tableViewWith*2, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0) style:UITableViewStylePlain];
self.tableThird.scrollEnabled = NO;
self.tableThird.delegate = self;
self.tableThird.dataSource = self;
self.tableThird.autoresizesSubviews = NO;
[self.tableThird setSeparatorColor:SeparatorColor];
self.thirdTableViewShow = NO;
[self.tableThird registerNib:[UINib nibWithNibName:product_TypeIdentifier bundle:nil] forCellReuseIdentifier:product_TypeIdentifier];
[self insertSubview:self.tableThird belowSubview:self.buttonBackView];
}
tableview的代理实现:
#pragma mark -- tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.cellHeight;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{
if (tableView == self.tableFirst) {
return self.dataSourceFirst.count;
}else if (tableView == self.tableSecond) {
return self.dataSourceSecond.count;
}else if (tableView == self.tableThird){
return self.dataSourceThird.count;
}
return 0;
}
- (void)changeCellTextColorWith:(UITableViewCell *)cell{
cell.textLabel.textColor = self.unSelectedColor ? self.unSelectedColor:[UIColor lightGrayColor];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
YDProduct_typeCell * cell = [tableView dequeueReusableCellWithIdentifier:product_TypeIdentifier forIndexPath:indexPath];
cell.cellTitleLabel.textColor = self.unSelectedColor ? self.unSelectedColor : [UIColor lightGrayColor];
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor = YDColor(248, 248, 248);
if (self.arrow) {
cell.cellArrowImage.hidden = NO;
}
else{
cell.cellArrowImage.hidden = YES;
}
if (tableView == self.tableFirst) {
cell.cellTitleLabel.text = self.dataSourceFirst[indexPath.row];
cell.cellTitleLabel.font = self.tableTitleFont ? [UIFont systemFontOfSize:self.tableTitleFont] : TableTitleDefalutFont;
[self changeCellTextColorWith:cell];
return cell;
}else if (tableView == self.tableSecond){
cell.cellTitleLabel.text = self.dataSourceSecond[indexPath.row];
cell.cellTitleLabel.font = self.tableTitleFont ? [UIFont systemFontOfSize:self.tableTitleFont] : TableTitleDefalutFont;
[self changeCellTextColorWith:cell];
return cell;
}else if (tableView == self.tableThird){
cell.cellTitleLabel.text = self.dataSourceThird[indexPath.row];
//cell3.selectionStyle = UITableViewCellSelectionStyleNone;
cell.cellTitleLabel.font = self.tableTitleFont ? [UIFont systemFontOfSize:self.tableTitleFont] : TableTitleDefalutFont;
[self changeCellTextColorWith:cell];
return cell;
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"selectButtonTagIs %li arrow is %li \n indexPath = %li",_lastSelectedButtonIndex,self.arrow,indexPath.row);
self.lastSelectedCellIndex = indexPath.row;
UIButton *btn = (UIButton *)[self viewWithTag:self.lastSelectedButtonIndex];
if (tableView == self.tableFirst) {
if (_lastSelectedButtonIndex == 700 && self.thirdTableViewShow == YES){
self.thirdTableViewShow = NO;
CGFloat TableViewW = self.cellHeight*self.dataSourceSecond.count;
CGFloat MaxHeigth = self.tableViewMaxHeight ? self.tableViewMaxHeight : tableViewMaxHeightDefalut;
//判断是否需要滑动
if (TableViewW > MaxHeigth) {
self.tableFirst.scrollEnabled = YES;
TableViewW = MaxHeigth;
}else{
self.tableFirst.scrollEnabled = NO;
}
self.tableSecond.frame = CGRectMake(self.tableViewWith, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
[UIView animateWithDuration:self.caverAnimationTime animations:^{
self.tableThird.frame = CGRectMake(self.tableViewWith * 2, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, 0);
self.tableSecond.frame = CGRectMake(self.tableViewWith, CGRectGetMaxY(self.buttonBackView.frame), self.tableViewWith, TableViewW);
}];
if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:firstIndex:andfirstIndexName:)]) {
[_delegate menuCellDidSelected:_lastSelectedButtonIndex firstIndex:self.lastSelectedCellIndex andfirstIndexName:_dataSourceFirst[self.lastSelectedCellIndex]];
}
}else if (self.arrow == NO && self.secondTableViewShow == NO) {
[btn setTitle:_dataSourceFirst[self.lastSelectedCellIndex] forState:UIControlStateNormal];
btn.selected = YES;
[self remover];
NSLog(@"SelectFirst RowName is : %@",_dataSourceFirst[self.lastSelectedCellIndex]);
if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:andCategoryName:)]) {
[_delegate menuCellDidSelected:_lastSelectedButtonIndex andCategoryName:_dataSourceFirst[self.lastSelectedCellIndex]];
}
}else{
if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:firstIndex:andfirstIndexName:)]) {
[_delegate menuCellDidSelected:_lastSelectedButtonIndex firstIndex:self.lastSelectedCellIndex andfirstIndexName:_dataSourceFirst[self.lastSelectedCellIndex]];
}
[self.tableSecond reloadData];
}
}else if (tableView == self.tableSecond) {
//如果第一级存在第二级菜单,并且已经有了第三级菜单||如果第一级存在第二级菜单,并且没有第三级菜单
if (self.arrow == YES) {
if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:secondIndex:andsecondIndexName:)]) {
[_delegate menuCellDidSelected:_lastSelectedButtonIndex secondIndex:self.lastSelectedCellIndex andsecondIndexName:_dataSourceSecond[self.lastSelectedCellIndex]];
}
}else if (self.hasThirdTableView == YES){
if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:secondIndex:andsecondIndexName:)]) {
[_delegate menuCellDidSelected:_lastSelectedButtonIndex secondIndex:self.lastSelectedCellIndex andsecondIndexName:_dataSourceSecond[self.lastSelectedCellIndex]];
}
}else{
[btn setTitle:_dataSourceSecond[self.lastSelectedCellIndex] forState:UIControlStateNormal];
btn.selected = YES;
[self remover];
NSLog(@"Selectsecond RowName is : %@",_dataSourceSecond[self.lastSelectedCellIndex]);
if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:andCategoryName:)]) {
[_delegate menuCellDidSelected:_lastSelectedButtonIndex andCategoryName:_dataSourceSecond[self.lastSelectedCellIndex]];
}
}
}else if (tableView == self.tableThird){
self.hasThirdTableView = NO;
if (self.arrow == NO) {
[btn setTitle:_dataSourceThird[self.lastSelectedCellIndex] forState:UIControlStateNormal];
btn.selected = YES;
[self remover];
NSLog(@"Selectthird RowName is : %@",_dataSourceThird[self.lastSelectedCellIndex]);
if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:andCategoryName:)]) {
[_delegate menuCellDidSelected:_lastSelectedButtonIndex andCategoryName:_dataSourceThird[self.lastSelectedCellIndex]];
}
}else{
if (_delegate && [_delegate respondsToSelector:@selector(menuCellDidSelected:thirdIndex:andthirdIndexName:)]) {
[_delegate menuCellDidSelected:_lastSelectedButtonIndex thirdIndex:self.self.lastSelectedCellIndex andthirdIndexName:_dataSourceThird[self.lastSelectedCellIndex]];
}
}
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0) {
return 1;
}
return 0;
}
//分割线顶头显示
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
[cell setSeparatorInset:UIEdgeInsetsZero];
}
//Prevent the cell from inheriting the Table View's margin settings(防止单元格继承表视图的边缘设置)
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
[cell setPreservesSuperviewLayoutMargins:NO];
}
//Explictly set your cell's layout margins(设置你的cell的布局空间)
if ([cell respondsToSelector:@selector(setLayoutMargins:)]){
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
在removeDropdownMenuNotifacation接收到要求隐藏下拉菜单时响应:
-(void)removeDropdownMenu:(NSNotification *)noti{
NSString * value = noti.object;
if ([value isEqualToString:@"1"]) {
[self remover];
}
}
#pragma mark -- 点击屏幕隐藏菜单
- (void)remover{
[self DefalutTriangleImage];
[self hideCarverView];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([touch.view isKindOfClass:[UIButton class]] || [NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCell"] || [NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
return NO;
}else{
return YES;
}
}
移除通知:
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:removeDropdownMenuNotifacation object:nil];
}
刷新tableview:
-(void)reloadTableView:(UITableView *)tableView{
[tableView reloadData];
}