iOS关于搜索不规则瀑布流布局的实现小结

  最近在项目开发中遇到了不规则搜索布局的问题。之前常用的解决方案是用一个tableview用一个循环动态的加载,唯一的缺点是需要动态的移除,其实也已经足够。https://download.csdn.net/download/kuuailetianzi/9905316,需要的话可以下载使用。请教了一下身边的美女同事,她那边给出的思路是用UICollectionView的复用来实现。解决方案如下。

  1、pod导入JQCollectionViewAlignLayout库

  2、核心代码如下

  

//此处用库对象JQCollectionViewAlignLayout

    JQCollectionViewAlignLayout * layout = [[JQCollectionViewAlignLayout alloc]init];

    layout.itemSize = CGSizeMake(K_CC_SCREEN_WIDTH, 40);

    //此处设置行间距

    layout.minimumLineSpacing = 10;

    //此处设置列间距

    layout.minimumInteritemSpacing = 10;

    

    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];

    [self.bottomView addSubview:self.collectionView];

    

    self.collectionView.delegate = self;

    self.collectionView.dataSource = self;

    self.collectionView.layer.masksToBounds = YES;

    self.collectionView.layer.cornerRadius = 8.0;

    self.collectionView.showsVerticalScrollIndicator = NO;

    self.collectionView.showsHorizontalScrollIndicator = NO;

    self.collectionView.backgroundColor = [UIColor clearColor];

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.width.mas_equalTo(K_CC_SCREEN_WIDTH);

        make.bottom.mas_equalTo(self.bottomView);

        make.left.mas_equalTo(0);

        make.top.mas_equalTo(60);

    }];

    

    self.collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);

    

    [self.collectionView registerClass:[CCHighSeasPoolReturnCell class] forCellWithReuseIdentifier:kCellReuseIdentifier];

    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooterReuseIdentifier];

 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    CCHighSeasPoolReturnCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellReuseIdentifier forIndexPath:indexPath];

    

    NSString *codevalue= self.data[indexPath.section].items[indexPath.item].itemtitle;

    

    //选中项索引,修改背景、文本颜色,并给变量赋值

    if (indexPath.item==self.selectItemIndex) {

        cell.contentView.backgroundColor = K_CC_COLOR_STRING(@"#343E79");

        cell.lblTitle.textColor = K_CC_COLOR_STRING(@"#FFFFFF");

        

        self.rejectreason=codevalue;

    }else{

        cell.contentView.backgroundColor = K_CC_COLOR_STRING(@"#F5F5F5");

        cell.lblTitle.textColor = K_CC_COLOR_STRING(@"#333333");

    }

 

    cell.title = codevalue;

    return cell;

}

//初始化数组数据

-(void)loadData{

    if (!_data) {

        NSMutableArray *data = [[NSMutableArray alloc] init];

        //JQCollectionViewItemsVerticalAlignment verticalAlignments[] = {JQCollectionViewItemsVerticalAlignmentCenter, JQCollectionViewItemsVerticalAlignmentTop, JQCollectionViewItemsVerticalAlignmentBottom};

        //设置每一项水平从左到右对齐

        JQCollectionViewItemsHorizontalAlignment horizontalAlignments[] = {JQCollectionViewItemsHorizontalAlignmentCenter, JQCollectionViewItemsHorizontalAlignmentLeft, JQCollectionViewItemsHorizontalAlignmentRight, JQCollectionViewItemsHorizontalAlignmentFlow};

        //JQCollectionViewItemsDirection directions[] = {JQCollectionViewItemsDirectionLTR, JQCollectionViewItemsDirectionRTL};

        for (int i = 0; i < 1; i++) {

            JQCollectionViewItemsHorizontalAlignment horizontal = horizontalAlignments[1];

            

                    NSInteger count = self.dataAddressList.count;

                    NSMutableArray *items = [[NSMutableArray alloc] init];

 

                    for (int j = 0; j < count; j++) {

                        UIColor *color = K_CC_COLOR_STRING(@"#F5F5F5");

                        

                        NSDictionary *dic=[self.dataAddressList objectAtIndex:j];

                        NSString *codevalue=[dic objectForKey:@"codevalue"];

                        //宽度自适应

                        CGFloat labelW = [CCCommonAPI getWidthWithString:codevalue font:K_CC_FONT(14) constraintSize:CGSizeMake(MAXFLOAT, 40.0f)];

                        //此处为了给每一个项留一定的空白

                        CGFloat width = labelW+10;

 

                        CGSize size = CGSizeMake(width, 40);

                        

                        CCHighSeasPoolReturnItemModel *item = [[CCHighSeasPoolReturnItemModel alloc] initWithColor:color size:size index:j];

                        

                        item.itemtitle=codevalue;

                        

                        [items addObject:item];

                    }

            CCHighSeasPoolReturnModel *section = [[CCHighSeasPoolReturnModel alloc] init];

//                    section.verticalAlignment = vertical;

                    section.horizontalAlignment = horizontal;

                    //section.direction = JQCollectionViewItemsDirectionLTR;

                    section.items = items;

                    [data addObject:section];

//                }

//            }

            _data = data;

        }

    }

}

原文地址:https://www.cnblogs.com/bigant9527/p/14602495.html