iOS--簡易型標(biāo)簽的實(shí)現(xiàn)(UICollectionView)

接到一個需求就是要實(shí)現(xiàn)標(biāo)簽組的顯示和選擇是尖,如下圖所示:


image

一開始感覺沒有什么頭緒饺汹,參考網(wǎng)上各種demo痰催,發(fā)現(xiàn)大部分的demo都是以自繪制標(biāo)簽為主實(shí)現(xiàn)標(biāo)簽的長度計算和自動換行,但是這樣需要實(shí)現(xiàn)的計算量就非常大逸吵,對于一部分參考和后期維護(hù)起來就非常麻煩扫皱,稍微修改錯一個參數(shù),導(dǎo)致計算不準(zhǔn)確编检,這就不太好實(shí)現(xiàn)扰才。

但是想了一下我們常用的系統(tǒng)控件中衩匣,是否有相關(guān)的控件可以實(shí)現(xiàn)呢?第一個想法就讓我想到了UICollectionView,既然UICollectionView能實(shí)現(xiàn)瀑布流生百,為什么標(biāo)簽這種無規(guī)則的界面不能實(shí)現(xiàn)呢柄延?搜吧,于是就開始初步搭建:

首先,先了解在UICollectionView中如何能或者每一個cell的大小摆昧,想到我們常用的UITableView的操作蜒程,其實(shí)兩者的用法基本也是一樣的昭躺,所以在UICollectionViewDelegate中就有

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString * containString = @"你想知道怎么實(shí)現(xiàn)寬度計算嗎?";
    CGFloat width = [NSString getWidthWithText:containString height:30 font:13];
    return CGSizeMake(width + 25, 30.0f);
}

這個方法能手動設(shè)置cell的大小汉规。
既然能手動設(shè)置cell的大小了针史,那這樣實(shí)現(xiàn)起來就容易了碟狞,按照正常的流程設(shè)置UICollectionView的數(shù)據(jù)源方法和相關(guān)的代理方法即可。

接下來的就是一個封裝好的類频祝,即計算String的寬度:

/**
 根據(jù)高度度求寬度

 @param text 計算的內(nèi)容
 @param height 計算的高度
 @param font 字體大小
 @return 返回寬度
 */
+ (CGFloat)getWidthWithText:(NSString *)text height:(CGFloat)height font:(CGFloat)font
{
    //加上判斷常空,防止傳nil等不符合的值,導(dǎo)致程序奔潰
    if (text == nil || [text isEqualToString:@""]){
        text = @"無";
    }
    if (font <= 0){
        font = 13;
    }
    if (height < 0){
        height = 0;
    }
    CGRect rect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}
                                     context:nil];
    return rect.size.width;
}

這樣大概的設(shè)置昆禽,大概即可實(shí)現(xiàn)在UICollectionView中的每一個cell的大小醉鳖,而最重要的一步就來了盗棵,如何設(shè)置UICollectionView的布局呢纹因?,這個就是整個標(biāo)簽組最為重要的一部分。
1.如何實(shí)現(xiàn)cell的布局位置要靠左對齊寄疏,并實(shí)現(xiàn)到屏幕最右邊時能自動換行陕截;
2.如何實(shí)現(xiàn)每一個不同長度的cell的具體距離和到不會被UICollectionViewFlowLayout默認(rèn)數(shù)據(jù)自動拉伸到屏幕平分呢批什;

這個時候驻债,我們就需要重寫UICollectionViewFlowLayout,首先要獲取相關(guān)容器的寬度暮的,和或者每一個cell的寬度淌实,然后通過計算每一個行cell的寬度和間隙相加的和是否大于容器的寬度,如果大于恨闪,即可更換其行數(shù)咙咽,以下就是 .m文件的直接代碼展示(參考來源 @Giovanni Lodi):

@interface UICollectionViewLayoutAttributes (LeftAligned)

- (void)leftAlignFrameWithSectionInset:(UIEdgeInsets)sectionInset;

@end

@implementation UICollectionViewLayoutAttributes (LeftAligned)

- (void)leftAlignFrameWithSectionInset:(UIEdgeInsets)sectionInset
{
    CGRect frame = self.frame;
    frame.origin.x = sectionInset.left;
    self.frame = frame;
}

@end

#pragma mark -

@implementation UICollectionViewLeftAlignedLayout

#pragma mark - UICollectionViewLayout

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray *originalAttributes = [super layoutAttributesForElementsInRect:rect];
    NSMutableArray *updatedAttributes = [NSMutableArray arrayWithArray:originalAttributes];
    for (UICollectionViewLayoutAttributes *attributes in originalAttributes) {
        if (!attributes.representedElementKind) {
            NSUInteger index = [updatedAttributes indexOfObject:attributes];
            updatedAttributes[index] = [self layoutAttributesForItemAtIndexPath:attributes.indexPath];
        }
    }

    return updatedAttributes;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes* currentItemAttributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy];
    UIEdgeInsets sectionInset = [self evaluatedSectionInsetForItemAtIndex:indexPath.section];

    BOOL isFirstItemInSection = indexPath.item == 0;
    CGFloat layoutWidth = CGRectGetWidth(self.collectionView.frame) - sectionInset.left - sectionInset.right;

    if (isFirstItemInSection) {
        [currentItemAttributes leftAlignFrameWithSectionInset:sectionInset];
        return currentItemAttributes;
    }

    NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];
    CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame;
    CGFloat previousFrameRightPoint = previousFrame.origin.x + previousFrame.size.width;
    CGRect currentFrame = currentItemAttributes.frame;
    CGRect strecthedCurrentFrame = CGRectMake(sectionInset.left,
                                              currentFrame.origin.y,
                                              layoutWidth,
                                              currentFrame.size.height);
    // if the current frame, once left aligned to the left and stretched to the full collection view
    // width intersects the previous frame then they are on the same line
    BOOL isFirstItemInRow = !CGRectIntersectsRect(previousFrame, strecthedCurrentFrame);

    if (isFirstItemInRow) {
        // make sure the first item on a line is left aligned
        [currentItemAttributes leftAlignFrameWithSectionInset:sectionInset];
        return currentItemAttributes;
    }

    CGRect frame = currentItemAttributes.frame;
    frame.origin.x = previousFrameRightPoint + [self evaluatedMinimumInteritemSpacingForSectionAtIndex:indexPath.section];
    currentItemAttributes.frame = frame;
    return currentItemAttributes;
}

- (CGFloat)evaluatedMinimumInteritemSpacingForSectionAtIndex:(NSInteger)sectionIndex
{
    if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]) {
        id<UICollectionViewDelegateLeftAlignedLayout> delegate = (id<UICollectionViewDelegateLeftAlignedLayout>)self.collectionView.delegate;

        return [delegate collectionView:self.collectionView layout:self minimumInteritemSpacingForSectionAtIndex:sectionIndex];
    } else {
        return self.minimumInteritemSpacing;
    }
}

- (UIEdgeInsets)evaluatedSectionInsetForItemAtIndex:(NSInteger)index
{
    if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
        id<UICollectionViewDelegateLeftAlignedLayout> delegate = (id<UICollectionViewDelegateLeftAlignedLayout>)self.collectionView.delegate;

        return [delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:index];
    } else {
        return self.sectionInset;
    }
}
@end

那么既然最困難的一步就是布局的問題已經(jīng)解決了,那整體的頁面顯示已經(jīng)完成了余素,那接下來就是數(shù)據(jù)的處理了,那要如何實(shí)現(xiàn)下圖顯示炊昆?


標(biāo)簽選擇.gif

那首先要思考兩個問題凤巨,如果通過對數(shù)據(jù)源的設(shè)置實(shí)現(xiàn)UICollectionView中每一組選中cell或者不選中cell敢茁,多選和單選的區(qū)別,此時我想到的一個方法就是給數(shù)據(jù)源一個屬性伸刃,標(biāo)識該cell是否選中:

/**
 是否選中
 */
@property (nonatomic, assign) BOOL isSelect;

通過數(shù)據(jù)源的屬性來控制cell內(nèi)部選中的控件的顏色和狀態(tài)

    [_tagBtn setTitle:model.name forState: UIControlStateNormal];
    UIColor *containStringColor = model.color == 0 ? tagBlueColor : model.color == 1 ? tagRedColor : model.color == 2 ? tagGreenColor : model.color == 3 ? tagYellowColor : model.color == 4 ? tagVioletColor : tagIndigoColor;
    if (model.isSelect == YES)
    {
        [_tagBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _tagBtn.backgroundColor = containStringColor;
    }
    else
    {
        [_tagBtn setTitleColor:labelBlackColor forState:UIControlStateNormal];
        _tagBtn.backgroundColor = HEXCOLOR(0xeeeeee);
    }

那controller中的數(shù)據(jù)該怎么判斷是否選中和未選擇捧颅,然后實(shí)現(xiàn)給數(shù)據(jù)源的屬性選中呢较雕?具體思路就是:需要進(jìn)行遍歷數(shù)據(jù)源中的所有模型進(jìn)行判斷和賦值選中狀態(tài),實(shí)現(xiàn)方法如下:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    
    PBCTagGroundModel *groundModel = self.listArrM[indexPath.section];

    DLog(@"indexPath.Seciont = %zd,row = %zd",indexPath.section,indexPath.row);
    
    //替換成可選模式
    for (int i = 0; i < groundModel.tags.count; ++i)
    {
        PBCTagModel *model = groundModel.tags[i];
        
        if (indexPath.row == i)
        {
            if (model.isSelect == YES)
            {
                model.isSelect = NO;
            }
            else
            {
                model.isSelect = YES;
            }
            
            [groundModel.tags replaceObjectAtIndex:i withObject:model];
        }
    }
    [self.listArrM replaceObjectAtIndex:indexPath.section withObject:groundModel];
    [self.collectionView reloadData];
}

既然每一組的cell都實(shí)現(xiàn)了選中和未選中狀態(tài)了,那如何實(shí)現(xiàn)判斷全選和不選中的狀態(tài)呢激捏?這里就需要給UICollectionView的頭部設(shè)置一個點(diǎn)擊事件凄吏,并且也需要對數(shù)據(jù)源中的所有數(shù)據(jù)進(jìn)行遍歷,對數(shù)據(jù)的選中狀態(tài)進(jìn)行選中和未選中的屬性賦值图柏。

那么問題來了蚤吹,如何這是UICollectionView的頭部呢?這個和UITableView的操作其他大有雷同:
首先繁涂,要先注冊UICollectionView的頭部:

    //注冊頭視圖
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionViewHeader"];

然后實(shí)現(xiàn)collectionView的頭部尾部設(shè)置代理

//設(shè)置頭視圖的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, 44);
}

//創(chuàng)建頭視圖
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    
    
    NSString *indentifierString = @"UICollectionViewHeader";
    
    UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                            withReuseIdentifier:indentifierString
                                                                                   forIndexPath:indexPath];
    headView.backgroundColor = [UIColor whiteColor];
    //此處操作是為了防止頭部視圖重復(fù)使用(從緩存機(jī)制中取出),導(dǎo)致重疊視圖扔罪,如要移除此前設(shè)置的視圖再進(jìn)行繪制
    for (UIView *subView in headView.subviews)
    {
        [subView removeFromSuperview];
    }

   //設(shè)置相關(guān)視圖
   //.....此處設(shè)置
    return headView;
}

設(shè)置完頭部視圖了矿酵,那具體的數(shù)據(jù)操作就是遍歷數(shù)據(jù)修改數(shù)據(jù)源狀態(tài)了矗积,代碼如下:

/**
 點(diǎn)擊選擇組便簽

 @param btn 組標(biāo)簽
 */
- (void)clickSelectAllBtn:(UIButton *)btn
{
    btn.selected = !btn.selected;
    PBCTagGroundModel *groundModel = self.listArrM[btn.tag];
    
    //替換成可選模式
    for (int i = 0; i < groundModel.tags.count; ++i)
    {
        PBCTagModel *model = groundModel.tags[i];
        if (btn.selected == YES)
        {
            model.isSelect = YES;
        }
        else
        {
            model.isSelect = NO;
        }
        
        [groundModel.tags replaceObjectAtIndex:i withObject:model];
    }
    [self.listArrM replaceObjectAtIndex:btn.tag withObject:groundModel];
    [self.collectionView reloadData];
}

那么對數(shù)據(jù)源也處理完了棘捣,整體的標(biāo)簽組也是實(shí)現(xiàn)了。

其實(shí)整體設(shè)置標(biāo)簽組哪自,最大的難度是在于UICollectionViewFlowLayout的重寫和計算禁熏,如果這一步解決了瞧毙,整體的思路很清晰寄症,可以直接解決問題有巧。

以上就是通過UICollectionView來實(shí)現(xiàn)標(biāo)簽組的方法,可能實(shí)現(xiàn)的路徑和方法很多男图,也有更加多便捷方法和思路,上面方法如有不足之處望大家指出栈戳,或者有更優(yōu)的方法难裆,也歡迎大家來探討乃戈。

類似上圖介紹的demo地址:Demo地址

大千世界,求同存異缩歪;相遇是緣侦讨,相識是份韵卤,相知便是“猿糞”(緣分)
From MZou

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末沈条,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子屋厘,更是在濱河造成了極大的恐慌月而,老刑警劉巖父款,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件憨攒,死亡現(xiàn)場離奇詭異,居然都是意外死亡瞻坝,警方通過查閱死者的電腦和手機(jī)杏瞻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來勉痴,“玉大人蒸矛,你說我怎么就攤上這事≌都溃” “怎么了乡话?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵绑青,是天一觀的道長闸婴。 經(jīng)常有香客問我,道長降狠,這世上最難降的妖魔是什么庇楞? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任榜配,我火速辦了婚禮,結(jié)果婚禮上吕晌,老公的妹妹穿的比我還像新娘蛋褥。我一直安慰自己,他們只是感情好睛驳,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布壁拉。 她就那樣靜靜地躺著,像睡著了一般柏靶。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上溃论,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天,我揣著相機(jī)與錄音钥勋,去河邊找鬼炬转。 笑死辆苔,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的扼劈。 我是一名探鬼主播驻啤,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼荐吵!你這毒婦竟也來了骑冗?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤先煎,失蹤者是張志新(化名)和其女友劉穎贼涩,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體薯蝎,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡遥倦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了占锯。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片袒哥。...
    茶點(diǎn)故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖消略,靈堂內(nèi)的尸體忽然破棺而出堡称,到底是詐尸還是另有隱情,我是刑警寧澤疑俭,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布粮呢,位于F島的核電站,受9級特大地震影響钞艇,放射性物質(zhì)發(fā)生泄漏啄寡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一哩照、第九天 我趴在偏房一處隱蔽的房頂上張望挺物。 院中可真熱鬧,春花似錦飘弧、人聲如沸识藤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽痴昧。三九已至,卻和暖如春冠王,著一層夾襖步出監(jiān)牢的瞬間赶撰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留豪娜,地道東北人餐胀。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像瘤载,于是被迫代替她去往敵國和親否灾。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容