UICollectionView 解释:选项框
1.拖拽一个Collection View控件到现有的View上
2.连接
Collection View连接到代码中,命名为mainCollectionView
3.包含代理
<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
4.将代理设置为自己的
[self.mainTableView setDelegate:self];
[self.mainTableView setDataSource:self];
5.写下必选的代理函数
//行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 5;
}
//行内容
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
}
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//通过调节每一行的大小及.xlb中Cell的大小来使Cell显示
return CGSizeMake(110, 90);
}
6.实现上述代理函数
事先要先自定义一个UICollectionViewCell的子类,类名假设CollectionViewCell,并包含同名xib
第一步:导入头文件并在ViewDidLoad内加入自定义CELL xib的注册
[self.mainCollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"mainIdentifier"];
在行内容函数里
static NSString *identifer = @"mainIdentifier";
CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifer forIndexPath:indexPath];
//此处可以写cell的一些配置,内容,样式等
return cell;
若没有写Cell的配置等东西,则显示你在TableViewCell.xlb中设置的东西
若要更多功能,则增加代理
<UICollectionViewDelegate>
//点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"选中collectionViewCell :%ld", indexPath.row);
}