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

这里实现一个文字长度不等的collection布局, 可以做推荐等
先上效果
iOS 推荐标签_长度不等_collection布局_第1张图片

使用方法如下:


#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

你可能感兴趣的:(iOS,iOS源码)