UICollectionView basics, uicollectionview
UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
// Scroll direction
flowLayout.minimumLineSpacing =
10.0
;
// Line spacing (minimum)
flowLayout.minimumInteritemSpacing =
50.0
;
// Item spacing (minimum)
flowLayout.itemSize = CGSizeMake(
50
,
50
);
// Item size
flowLayout.sectionInset = UIEdgeInsetsMake(
10
,
10
,
10
,
10
);
// Set the margin of the section
flowLayout.headerReferenceSize = CGSizeMake(
320
,
20
);
flowLayout.footerReferenceSize = CGSizeMake(
320
,
20
);
// The second parameter is the cell layout.
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(
0
,
0
,
320
,
568
) collectionViewLayout:flowLayout];
[flowLayout release];
collectionView.dataSource = self;
collectionView.delegate = self;
collectionView.backgroundColor = [UIColor orangeColor];
// 1 register and reuse cells (cell types and identifiers) (multiple reusable cells can be registered, so make sure the reuse identifiers are different) and register them in the reuse pool of collectionView.
[collectionView registerClass:[CLCollectionViewCell
class
] forCellWithReuseIdentifier:cellIdentifier];
// The first parameter: returned View type
// Second parameter: Set the View type (header, footer)
// The third parameter: Set the reuse identifier
[collectionView registerClass:[HeadView
class
] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
[collectionView registerClass:[FootView
class
] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier];
[self.view addSubview:collectionView];
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return
10
;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return
10
;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 2 find the cell from the reuse pool (1: cell identifier 2: indexPath determines that the system does not need to create a cell for you. If you do not need to create a cell, the previous cell will be used directly)
CLCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor yellowColor];
cell.textLabel.text = [NSString stringWithFormat:@
"%ld"
, (
long
)indexPath.row];
return
cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if
(kind == UICollectionElementKindSectionHeader) {
HeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
headerView.textLabel.text = @
"Let me form the header! "
;
headerView.textLabel.textAlignment = NSTextAlignmentCenter;
headerView.textLabel.textColor = [UIColor whiteColor];
return
headerView;
}
FootView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerIdentifier forIndexPath:indexPath];
footerView.textLabel.text = @
"Let me form the end! "
;
footerView.textLabel.textAlignment = NSTextAlignmentCenter;
footerView.textLabel.textColor = [UIColor whiteColor];
return
footerView;
}
#pragma mark - UICollectionViewDelegate
- (
void
)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@
"%d %d"
, indexPath.section, indexPath.row);
}
#pragma mark - UICollectionViewDelegateFlowLayout
/*
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
}
*/
[collectionView registerClass:[FootView
class
] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier];
[self.view addSubview:collectionView];