新建cellectionViewide
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];spa
CollectionViewController *vc = [[CollectionViewController alloc]initWithCollectionViewLayout:layout];3d
2. 設置不顯示滾動條orm
self.collectionView.showsHorizontalScrollIndicator = NO;ci
self.collectionView.showsVerticalScrollIndicator = NO;rem
3. 移除cell的子控件it
當每一個cell中的子控件都不同的時候,咱們複用cell以前,須要移除cell裏邊的全部子控件io
while (cell.contentView.subviews.firstObject) {im
[cell.contentView.subviews.firstObject removeFromSuperview];layout
}
4. 設置間距
//定義每一個UICollectionView 的間距(返回UIEdgeInsets:上、左、下、右)
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(kStatus_Bar_Height, kGrdGap_Crdgap, kStatus_Bar_Height, kGrdGap_Crdgap);
}
// 設置兩列之間的最小間隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return kGrdGap_Crdgap;
}
// 設置最小行間距,也就是前一行與後一行的中間最小間隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return kStatus_Bar_Height;
}
5. 設置cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CardStyleModel *csm = self.responseModel.cardStyle;
CGFloat cardWidth = 0;
if ((self.toInterfaceOrientation == UIInterfaceOrientationPortrait) || (self.toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) {
cardWidth = kScreen_Width - 2 * kGrdGap_Crdgap;
}else if((self.toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (self.toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)){
cardWidth = (kScreen_Width - 3 * kGrdGap_Crdgap) * 0.5;
}
CGFloat cellWidth = cardWidth / [csm.colCount intValue];
CGFloat cellHeight = cellWidth;
CGFloat cardHeight = cellHeight * [csm.rowCount intValue];
return CGSizeMake(cardWidth, cardHeight);
}