UICollectionView 在iOS開發(fā)中也是非常常用的控件跃巡,例如市面上最流行的淘寶,京東都有用到UICollectionView來寫頁面猎醇,一些好看的瀑布流也是采用UICollectionView來實現(xiàn)充坑,總體來說UICollectionView用起來還是很實用方便的熔任;
UICollectionView
創(chuàng)建
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
CGFloat itemWidth = (MWidth - 40) / 3;
//設置單元格大小
layout.itemSize = CGSizeMake(itemWidth, itemWidth / 1.6);
//最小行間距(默認為10)
layout.minimumLineSpacing = 10;
//最小item間距(默認為10)
layout.minimumInteritemSpacing = 10;
//設置UICollectionView的滑動方向
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//設置UICollectionView的間距
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, MWidth, MHeight - 80 - 220 - 50 - 40) collectionViewLayout:layout];
//遵循CollectionView的代理方法
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor clearColor];
//注冊cell
[_collectionView registerClass:[DiscoverRechangeCenterCell class] forCellWithReuseIdentifier:@"DiscoverRechangeCenterCell"];
[self.centerView addSubview:self.collectionView];
UICollectionView
的代理方法
#pragma mark CollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.sourceArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
DiscoverRechangeCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverRechangeCenterCell" forIndexPath:indexPath];
//實現(xiàn)cell的展示效果
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//實現(xiàn)cell的點擊方法
}