iOS 推荐标签_长度不等_collection布局

这里实现一个文字长度不等的collection布局, 可以做推荐等
先上效果
这里写图片描述

使用方法如下:


#import "ViewController.h"
#import "ItemData.h"
#import "EqualSpaceFlowLayout.h"
#import "CustomCollectionViewCell.h"
#import "NSString+Extension.h"

@interface ViewController ()<EqualSpaceFlowLayoutDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) UIButton *selectCellBtn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *str = @"Corey_Jia";

    self.dataArray = [[NSMutableArray alloc] init];
    for (int i = 0;i < 50;i++) {
        ItemData *itemData = [[ItemData alloc] init];
        itemData.content = [str substringToIndex:arc4random()%9];
        itemData.size = [itemData.content sizeWithFont:[UIFont systemFontOfSize:13]];
        itemData.index = i;
        [self.dataArray addObject:itemData];
    }

    EqualSpaceFlowLayout *flowLayout = [[EqualSpaceFlowLayout alloc] init];
    flowLayout.delegate = self;

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = NO;
    [self.view addSubview:self.collectionView];

    [self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"CellIdentifier"];
    //获取某个cell  假如需要根据最后一个cell的frame来设置view的高度  可以这么获取
    UICollectionViewCell *cell = [self collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:49 inSection:0]];
    CGFloat collectionViewHeight = CGRectGetMaxY(cell.frame);
}

#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.dataArray count];
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *moreCellIdentifier = @"CellIdentifier";
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:moreCellIdentifier forIndexPath:indexPath];

    ItemData *itemData = [self.dataArray objectAtIndex:[indexPath row]];
    [cell.titleBtn setTitle:[NSString stringWithFormat:@"%@ >",itemData.content] forState:UIControlStateNormal];
    [cell.titleBtn addTarget:self action:@selector(cellClickWithBtn:) forControlEvents:UIControlEventTouchUpInside];
    cell.titleBtn.tag = itemData.index;
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ItemData *itemData = [self.dataArray objectAtIndex:[indexPath row]];
    return CGSizeMake(itemData.size.width+20, 32);
}

- (void)cellClickWithBtn:(UIButton *)button{
    //需要点击后做的事
    button.selected = YES;
}
@end

下载地址:http://download.csdn.net/detail/corey_jia/9425824

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值