UICollectionView

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值