iOS 推荐标签_长度不等_collection布局
来源:互联网 发布:c语言贪吃蛇源代码思路 编辑:程序博客网 时间:2024/06/11 20:31
这里实现一个文字长度不等的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
- iOS 推荐标签_长度不等_collection布局
- 利用【流式布局】快速实现动态添加长度不等的标签
- 标签布局(等间隙与间隙不等两种情况)
- Java集合框架_三_Collection接口
- IOS开发 把长度不等的UICollectionView 的cell 居左
- 155_集合_Collection集合的常用方法详解_03_contains
- iOS根据标签不同长度内容按钮自动换行
- 【字符串转置】 字符串包含长度不等的字符
- php 中字符串一样但长度不等的问题
- iOS 自定义不等高的cell
- 标签布局
- 数组类重载_中括号_等号_逻辑等_裸机不等
- 【学习笔记】读项亮的《推荐系统实践》_第四章利用用户标签数据
- IOS开发-16.自定义不等高cell
- iOS开发 - 处理不等高TableViewCell的招术
- 推荐系统_电影推荐
- 求两个字符串第一个不等的字符位置(长度)
- 当各个input框前面名字长度不等时,让上下input框对齐方法
- 3.1.1 如何选择全扫描操作
- 华为机试题:密码验证合格程序
- navicat 修改数据库字符集、字符排序规则、数据库引擎
- linux rpm安装 php mysql apche
- Android优化adapter及SparseArray介绍
- iOS 推荐标签_长度不等_collection布局
- 3.1.2 全扫描与舍弃
- 3.1.3 全扫描与多块读取
- c#的aspx页面中引用了用户控件,如何相互调用后台方法
- hadoop1 rpc
- 3.1.4 全扫描与高水位线
- 聊聊并发(1):深入分析Volatile的实现原理
- 闪屏页开发
- 3.2.2 索引扫描类型