多條件篩選下拉菜單价淌,在很多應(yīng)用中都有使用秒咐。例如:京東吏奸、美團(tuán)等黎休。我們先看一下運(yùn)行效果:
接下來(lái)我會(huì)針對(duì)里面主要代碼進(jìn)行講解
首先創(chuàng)建出篩選條件類浓领,在創(chuàng)建下拉菜單控件的時(shí)候把這個(gè)存放該類數(shù)組傳進(jìn)去,創(chuàng)建對(duì)應(yīng)標(biāo)題的按鈕
- (instancetype)initWithFrame:(CGRect)frame modelArr:(NSArray *)modelArr;
//篩選類
@interface Model : NSObject
@property (nonatomic, strong) NSString *title; //條件標(biāo)題
@property (nonatomic, assign) DropDownMenuViewType type; //彈出視圖類型
@property (nonatomic, assign) BOOL isSelect; //當(dāng)前標(biāo)題是否被選中
@property (nonatomic, assign) NSInteger fileterIndex; //選中該標(biāo)題中的某個(gè)條件的下標(biāo) 默認(rèn)選中第一個(gè)
@property (nonatomic, strong) NSArray *fileterArr; //該標(biāo)題下的查詢條件數(shù)組
@end
當(dāng)出現(xiàn)tableView的時(shí)候势腮,阻斷其他按鈕的點(diǎn)擊事件
//當(dāng)前tableView已經(jīng)出現(xiàn)的時(shí)候 點(diǎn)擊其他的條件選擇按鈕沒(méi)有效果
for (Model *tempModel in self.modelArr) {
if (tempModel.isSelect && index != self.selectBtnIndex) {
return;
}
}
因?yàn)橄旅娴暮谏幱安糠痔砑拥挠惺謩?shì)點(diǎn)擊事件联贩,所以他會(huì)跟UITableView和UICollectionView中的cell點(diǎn)擊事件發(fā)生沖突,通過(guò)實(shí)現(xiàn)手勢(shì)代理來(lái)解決這個(gè)問(wèn)題捎拯,具體代碼如下:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//獲取當(dāng)前手勢(shì)點(diǎn)擊的視圖:他會(huì)是UITableViewCell或者UICollectionViewCell的內(nèi)容視圖
//然后獲取到他的父視圖 可以找到你自己定義的cell類
//通過(guò)判斷類型來(lái)阻斷手勢(shì)觸發(fā)
if ([touch.view.superview isKindOfClass:[UITableViewCell class]] || [touch.view.superview isKindOfClass:[MyCollectionViewCell class]]) {
return NO;
}
return YES;
}
最后附上代碼的下載地址:
https://github.com/liuxiaoxin369/DropDownMenuView