效果圖:
.h文件里面要實(shí)現(xiàn)的協(xié)議的方法
在YDDropdownMenuDelegate里面要實(shí)現(xiàn)的協(xié)議方法有以下幾種:
/**
* net代理方法返回 菜單下標(biāo):index 菜單標(biāo)題:MenuTitle
*/
- (void)netClickMenuIndex:(NSInteger)menuIndex menuTitle:(NSString *)menuTitle;
/**
* 代理方法返回 菜單下標(biāo):index 一級(jí)菜單index:firstIndex 一級(jí)菜單名
*/
- (void)menuCellDidSelected:(NSInteger)MenuTitleIndex firstIndex:(NSInteger)firstIndex andfirstIndexName:(NSString *)firstIndexName;
/**
* 代理方法返回 菜單下標(biāo):index 二級(jí)菜單index:firstIndex 二級(jí)菜單名
*/
- (void)menuCellDidSelected:(NSInteger)MenuTitleIndex secondIndex:(NSInteger)secondIndex andsecondIndexName:(NSString *)secondIndexName;
/**
*代理方法返回 菜單下標(biāo):index 三級(jí)菜單index:firstIndex 三級(jí)菜單名
*/
- (void)menuCellDidSelected:(NSInteger)MenuTitleIndex thirdIndex:(NSInteger)thirdIndex andthirdIndexName:(NSString *)thirdIndexName;
/**
* 代理方法返回,選中的category篩選產(chǎn)品
*/
-(void)menuCellDidSelected:(NSInteger)MenuTitleIndex andCategoryName:(NSString *)categoryName;
/**
* 代理方法返回计螺,將本身視圖顯示在最上層
*/
-(void)bringselfViewToFront;
/**
* 代理方法返回珊搀,將本身視圖顯示在最collectionview下層
*/
-(void)bringselfViewToBackCollection;
定義的一些常用屬性(可在調(diào)用時(shí)設(shè)置冶忱,如若不設(shè)置,則會(huì)調(diào)用默認(rèn)的值):
/** 遮蓋層顏色 */
@property (nonatomic, strong) UIColor * CarverViewColor;
/** 自定義tag值防止和頁面其他tag有沖突 */
@property (nonatomic, assign) NSInteger menuButtonTag;
/** 遮蓋的動(dòng)畫時(shí)間 */
@property (nonatomic, assign) CGFloat caverAnimationTime;
/** 縮進(jìn)的動(dòng)畫時(shí)間 */
@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最大高度(超出高度可以滑動(dòng)顯示) */
@property (nonatomic, assign) CGFloat tableViewMaxHeight;
/** 未選中字體的顏色 */
@property (nonatomic, strong) UIColor * unSelectedColor;
/** 選中字體的顏色 */
@property (nonatomic, strong) UIColor * selectedColor;
/** 選中背景圖片 */
@property (nonatomic, strong) UIImage * selectedImage;
/** 設(shè)置代理 */
@property (nonatomic,weak) id <YDDropdownMenuDelegate> delegate;
@property (nonatomic,strong) UITableView * tableFirst;
@property (nonatomic,strong) UITableView * tableSecond;
@property (nonatomic,strong) UITableView * tableThird;
在初始化各個(gè)下拉的tableview時(shí)調(diào)用以下的方法進(jìn)行創(chuàng)建:
#pragma mark -- net一級(jí)一級(jí)點(diǎn)擊網(wǎng)絡(luò)獲取數(shù)據(jù)創(chuàng)建
/** net創(chuàng)建下拉菜單 */
- (void)netCreateMenuTitleArray:(NSArray *)menuTitleArray;
/** net導(dǎo)入一級(jí)菜單數(shù)據(jù) */
- (void)netLoadFirstArray:(NSArray *)firstArray andMenuIndex: (NSInteger)menuIndex andArrow:(BOOL)arrow;
/** net導(dǎo)入二級(jí)菜單數(shù)據(jù) */
- (void)netLoadSecondArray:(NSArray *)SecondArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow;
/** net導(dǎo)入三級(jí)菜單數(shù)據(jù) */
- (void)netLoadThirdArray:(NSArray *)ThirdArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow;
-(void)reloadTableView:(UITableView *)tableView;
.m文件里面要實(shí)現(xiàn)的方法
宏定義的一些默認(rèn)值(在用戶初始化不設(shè)置時(shí)調(diào)用):
#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; //三角形圖標(biāo)
@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
設(shè)置數(shù)組懶加載:
-(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通知境析,在外部發(fā)送通知隱藏下拉菜單時(shí)接收通知囚枪,對(duì)tableview菜單進(jìn)行隱藏:
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeDropdownMenu:) name:removeDropdownMenuNotifacation object:nil];
}
return self;
}
創(chuàng)建下拉主菜單(循環(huán)創(chuàng)建需要的button個(gè)數(shù)):
#pragma mark -- net一級(jí)一級(jí)點(diǎn)擊網(wǎng)絡(luò)獲取數(shù)據(jù)創(chuàng)建
- (void)netCreateMenuTitleArray:(NSArray *)menuTitleArray{
self.isNet = YES;
self.menuBaseHeight = 18;
self.tableViewMaxHeight = self.cellHeight * 6;
[self createMenuViewWithData:menuTitleArray];
[self createTableViewFirst];
[self createTableViewSecond];
[self createTableViewThird];
}
創(chuàng)建每個(gè)button下可能需要的tableview菜單:
//創(chuàng)建第一級(jí)目錄的tableview
- (void)netLoadFirstArray:(NSArray *)firstArray andMenuIndex:(NSInteger)menuIndex andArrow:(BOOL)arrow{
self.arrow = arrow;
[self createWithFirstData:firstArray];
[self netShowFirstTableByMenuIndex:menuIndex];
}
//創(chuàng)建第二級(jí)目錄的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];
}
//創(chuàng)建第三級(jí)目錄的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];
}
}
當(dāng)需要顯示某個(gè)tableview時(shí)進(jìn)行調(diào)用:
//顯示第一級(jí)目錄的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;
//判斷是否需要滑動(dòng)
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);
}];
}
}
//顯示第二級(jí)目錄的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];
}
//顯示第三級(jí)目錄的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);
}];
}
}
當(dāng)需要隱藏tableview時(shí)進(jìn)行調(diào)用:
- (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];
}
創(chuàng)建分類按鈕:
#pragma mark -- 創(chuàng)建分類按鈕
-(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];
}
}
通過點(diǎn)擊某個(gè)button來顯示第一個(gè)tableview(第一次點(diǎn)擊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);
}];
}
}
真正實(shí)現(xiàn)創(chuàng)建tableview的方法(上面只是在調(diào)用此方法):
#pragma mark -- 創(chuàng)建tableview
//創(chuàng)建第一級(jí)目錄的tableview的實(shí)現(xiàn)方法
- (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];
}
//創(chuàng)建第二級(jí)目錄的tableview的實(shí)現(xiàn)方法
- (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];
}
//創(chuàng)建第三級(jí)目錄的tableview的實(shí)現(xiàn)方法
- (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的代理實(shí)現(xiàn):
#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;
//判斷是否需要滑動(dòng)
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) {
//如果第一級(jí)存在第二級(jí)菜單派诬,并且已經(jīng)有了第三級(jí)菜單||如果第一級(jí)存在第二級(jí)菜單,并且沒有第三級(jí)菜單
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(防止單元格繼承表視圖的邊緣設(shè)置)
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
[cell setPreservesSuperviewLayoutMargins:NO];
}
//Explictly set your cell's layout margins(設(shè)置你的cell的布局空間)
if ([cell respondsToSelector:@selector(setLayoutMargins:)]){
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
在removeDropdownMenuNotifacation接收到要求隱藏下拉菜單時(shí)響應(yīng):
-(void)removeDropdownMenu:(NSNotification *)noti{
NSString * value = noti.object;
if ([value isEqualToString:@"1"]) {
[self remover];
}
}
#pragma mark -- 點(diǎn)擊屏幕隱藏菜單
- (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];
}