UICollectionView簡介

1、集合視圖概述ide

     UICollectionView也稱集合視圖,是一種新的數據展現方式,簡單地能夠理解爲多列的UITableView。例如:iBooks的書架效果、購物網站的商品展現效果等等。佈局

     UICollectionView與UITableView的實現相似,都須要設置代理和數據源。相似的是它們都有cell,可是UICollectionView比UITableView的cell佈局更復雜,須要使用一個類描述集合視圖的佈局——UICollectionViewLayout。網站



2、建立UICollectionViewui

@property (retain, nonatomic) UICollectionView *collectionView;atom

@property (retain, nonatomic) NSMutableArray *modelArray;spa


一、建立並設置佈局對象--UICollectionViewFlowLayout代理

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];orm

    layout.itemSize = CGSizeMake(150, 90);  //設置cell大小
    layout.
minimumInteritemSpacing = 10;    //設置最小列間距
    layout.
minimumLineSpacing = 30;     //設置最小行間距
    layout.
sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); //設置分區上左下右距離
對象

    layout.scrollDirection = UICollectionViewScrollDirectionVertical;//滾動方向three

    layout.headerReferenceSize = CGSizeMake(0, 30);     //頁眉大小

    layout.footerReferenceSize = CGSizeMake(30, 30);    //頁腳大小


二、建立集合視圖,並指定佈局方式

    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];

    _collectionView.dataSource = self;
   
_collectionView.delegate = self;
   
_collectionView.backgroundColor = [UIColor redColor];

    [self.view addSubview:_collectionView];


三、註冊重用集合

    //建立CollectionViewCell是經過註冊cell來實現的

    [_collectionView registerClass:[YFMyCollectionViewCell class]forCellWithReuseIdentifier:@"cell"];

    //註冊header footer
    [
_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];


四、代理、數據源

//@required
//設置每一個分區item個數
- (
NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
return _modelArray.count;
}
//建立item並設置顯示內容
- (
UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
YFMyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
   
    cell.
contentView.backgroundColor = [UIColor whiteColor];
    cell.
label.text = [NSString stringWithFormat:@"S:%ld, R:%ld", indexPath.section, indexPath.row];
   
   
YFModel *model = _modelArray[indexPath.row];
    [cell.
imgView sd_setImageWithURL:[NSURL URLWithString:model.thumbURL]];
   
   
return cell;
}
//@optional
//設置分區個數
- (
NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
   
return 2;
}
//設置區頭、尾
- (
UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
   
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
       
//重用集合中取
       
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        headerView.
backgroundColor = [UIColor cyanColor];
       
return headerView;
    }
else {
       
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        footerView.
backgroundColor = [UIColor blackColor];
       
return footerView;
    }

}



3、代理、數據源協議

@protocol UICollectionViewDataSource <NSObject>
@required

- (
NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (
UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

@optional

- (
NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;

// The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
- (
UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;

@end



@protocol UICollectionViewDelegate <UIScrollViewDelegate>
@optional

// Methods for notification of selection/deselection and highlight/unhighlight events.
// The sequence of calls leading to selection from a user touch is:
//
// (when the touch begins)
// 1. -collectionView:shouldHighlightItemAtIndexPath:
// 2. -collectionView:didHighlightItemAtIndexPath:
//
// (when the touch lifts)
// 3. -collectionView:shouldSelectItemAtIndexPath: or -collectionView:shouldDeselectItemAtIndexPath:
// 4. -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath:
// 5. -collectionView:didUnhighlightItemAtIndexPath:
- (
BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (
void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (
void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (
BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
- (
BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath; // called when the user taps on an already-selected item in multi-select mode
- (
void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
- (
void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;

- (
void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);
- (
void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);
- (
void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;
- (
void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;

// These methods provide support for copy/paste actions on cells.
// All three should be implemented if any are.
- (
BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath;
- (
BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender;
- (
void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender;

// support for custom transition layout
- (
UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout;

@end


注意:代理中方法的優先級比setter方法的優先級高

@protocol UICollectionViewDelegateFlowLayout <UICollectionViewDelegate>
@optional

- (
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;

@end

相關文章
相關標籤/搜索