寫(xiě)在前面真仲,一直以來(lái)特別想寫(xiě)點(diǎn)東西。記得有次老大曾經(jīng)說(shuō)過(guò)一句話初澎,不要一直手心向上秸应,要說(shuō)會(huì)手心向下。懂得分享碑宴,好多次決定寫(xiě)文章软啼,可是擔(dān)心自己寫(xiě)出來(lái)的質(zhì)量不高,反反復(fù)復(fù)好幾次延柠。今日再次下決心開(kāi)啟的“半桶水”的分享祸挪。
好吧嘮叨了一大堆,直接進(jìn)入主題:
下面的這個(gè)就是我們?cè)僖彩煜げ贿^(guò)來(lái)的淘寶贞间,搜索結(jié)果頁(yè)的篩選頁(yè)面贿条,今天的主題就是實(shí)現(xiàn)這個(gè)樣的效果。
首先看看我的做出來(lái)效果
實(shí)現(xiàn)的原理
就是UICollectionView 的分組
主要想說(shuō)的是我靠的是數(shù)據(jù)刷新的頁(yè)面增热,首先來(lái)看看我的數(shù)據(jù)這里是自己造的數(shù)據(jù)也是模擬接口返回的那種
-(void)loadData{
self.dataArray = [NSMutableArray array];
NSMutableDictionary *mudic = [NSMutableDictionary dictionary];
mudic[@"switch"] = @"0";
mudic[@"name"] = @"教材分冊(cè)1";
NSMutableArray *arr = [NSMutableArray array];
for(int i = 0;i<20;i++){
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
dic[@"name"]=[NSString stringWithFormat:@"第%d章",i];
dic[@"id"]=[NSString stringWithFormat:@"%d",i];
dic[@"showclose"]=@"0";
[arr addObject:dic];
}
mudic[@"data"]=arr;
for(int i=0;i<10;i++){
[self.dataArray addObject:mudic];
}
self.scrArray = [self.dataArray mutableCopy];
}
1.這里的switch 是需要自己添加的字:控制右側(cè)的關(guān)閉展開(kāi)
2.內(nèi)層的showclose是控制里面的是否選中
3.self.scrArray 這個(gè)數(shù)組主要是保留一份最原始的數(shù)據(jù)源整以,而我們操作的主要是self.dataArray,接著往下看明白
接下來(lái)我們看好一下代理的實(shí)現(xiàn)
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
FiltrateReusableView *headReusableView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FiltrateReusableView" forIndexPath:indexPath];
headReusableView.index = indexPath.section;
headReusableView.dic = self.dataArray[indexPath.section];
headReusableView.filtrateReusableViewBlock = ^(BOOL state,NSInteger index){
NSDictionary *dic = self.dataArray[index];
NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:dic];
NSString *stateStr = @"0";
if(state){
stateStr = @"1";
}
mudic[@"switch"]=stateStr;
[self.dataArray replaceObjectAtIndex:index withObject:mudic];
NSIndexSet *set = [NSIndexSet indexSetWithIndex:index];
[self.collectView reloadSections:set];
};
return headReusableView;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return self.dataArray.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
NSDictionary *sedic = self.dataArray[section];
NSArray *arr = sedic[@"data"];
if([sedic[@"switch"] integerValue]==0&&arr.count>6){
return 6;
}
return arr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FiltrateCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FiltrateCVCell" forIndexPath:indexPath];
NSDictionary *sedic = self.dataArray[indexPath.section];
NSArray *arr = sedic[@"data"];
NSDictionary *item = arr[indexPath.row];
cell.dic = item;
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dic = self.dataArray[indexPath.section];
NSMutableDictionary *musecDic = [NSMutableDictionary dictionaryWithDictionary:dic];
NSArray *arr = musecDic[@"data"];
NSMutableArray *muArr = [NSMutableArray arrayWithArray:arr];
NSDictionary *item = muArr[indexPath.row];
NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:item];
if([mudic[@"showclose"] integerValue]==1){//已經(jīng)選中的
musecDic = self.scrArray[indexPath.section];
}else{
mudic[@"showclose"]=@"1";
musecDic[@"data"] = @[mudic];
}
[self.dataArray replaceObjectAtIndex:indexPath.section withObject:musecDic];
NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
[self.collectView reloadSections:set];
}
怎么樣是否感覺(jué)立刻清晰多了,靠更改數(shù)據(jù)刷新對(duì)應(yīng)的section峻仇。
好吧就這樣了公黑,感覺(jué)解析的語(yǔ)言比較少,突然想起來(lái)一句話彪悍的人生不需要解釋摄咆。啦啦啦啦啦凡蚜,稍后我會(huì)整理一個(gè)demo,當(dāng)然也會(huì)放上來(lái),哪里呢github 吭从,正好開(kāi)始自己的git旅程朝蜘,逼迫自己不斷前進(jìn)!
文章的菜單我也專(zhuān)門(mén)從項(xiàng)目中提取出來(lái)做了一個(gè)簡(jiǎn)單的YJDemo地址
后續(xù):
如果你覺(jué)得對(duì)你有用歡迎給我一個(gè)star 涩金,鼓勵(lì)一下自己以后越來(lái)越好谱醇,爭(zhēng)取提高分享更干的貨!