iOS开发基础 - UICollectionView

来源:互联网 发布:linux man 1 2 3 编辑:程序博客网 时间:2024/05/29 18:52

基本设置
//UITableView 表视图
//UICollectionView 集合视图 继承自UIScrollView

//UICollectionViewLayout 界面布局类 抽象类 通常不会直接使用它 而是使用它的子类//UICollectionViewFlowLayout 网格布局类//collectionView 主要是用来显示图片的UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc] init];//间隔设置flow.minimumInteritemSpacing = 0;flow.minimumLineSpacing = 0;//设置滚动方向flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;/*UICollectionViewScrollDirectionVertical,UICollectionViewScrollDirectionHorizontal */UICollectionView * collectionV = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];collectionV.delegate = self;collectionV.dataSource = self;[collectionV registerClass:[NewCell class] forCellWithReuseIdentifier:@"id"];collectionV.backgroundColor = [UIColor whiteColor];

协议方法设置 与tableView基本类似

-(CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout )collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

//设置cell的宽高/网格的宽高/项的宽高//celld 坐标的参考点 左侧的cell以左侧为参考点 右侧的cell以右侧为参考点return CGSizeMake(80, 120);

}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//每个分区多少项
return 10;
}
//创建cell 重用池
-(UICollectionViewCell )collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@”id” forIndexPath:indexPath];
cell.titleLabel.text = [NSString stringWithFormat:@”第%ld分区 第%ld行”,(long)indexPath.section,(long)indexPath.item];

return cell;

}

//设置cell之间横向的最小距离
//设置cell之间纵向的最小距离
-(CGFloat)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout )collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

return 0;

}
-(CGFloat)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout )collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 0;
}

//内容视图距离上左下右边框的内边距
-(UIEdgeInsets)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout )collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}

//选中cell
-(void)collectionView:(UICollectionView )collectionView didSelectItemAtIndexPath:(NSIndexPath )indexPath{

}
//反选cell
-(void)collectionView:(UICollectionView )collectionView didDeselectItemAtIndexPath:(NSIndexPath )indexPath{

}

//设置区头区尾的方法
//注册事件
[self.collectionV registerNib:[UINib nibWithNibName:@”NewCell” bundle:nil] forCellWithReuseIdentifier:@”id”];

[self.collectionV registerNib:[UINib nibWithNibName:@"HeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerId"];[self.collectionV registerNib:[UINib nibWithNibName:@"FooterView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerId"];

-(UICollectionReusableView )collectionView:(UICollectionView )collectionView viewForSupplementaryElementOfKind:(NSString )kind atIndexPath:(NSIndexPath )indexPath{

if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {    HeaderView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerId" forIndexPath:indexPath];    return headerView;}else{    FooterView * footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footerId" forIndexPath:indexPath];    return footerView;}

}

//设置区头区尾的高度
-(CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout )collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
return CGSizeMake(320, 50);
}

-(CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout )collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(320, 50);
}

0 0
原创粉丝点击