UIKit 框架之UICollectionView

1.自定義UICollectionViewCelldom

在myCollectionViewCell.h中聲明兩個屬性atom

//
//  myCollectionViewCell.h
//  UICollectionView
//
//  Created by City--Online on 15/5/25.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface myCollectionViewCell : UICollectionViewCell
@property(nonatomic,strong) UIImageView *myImageView;
@property(nonatomic,strong) UILabel *nameLabel;
@end

 在myCollectionViewCell.m中繪製視圖3d

//
//  myCollectionViewCell.m
//  UICollectionView
//
//  Created by City--Online on 15/5/25.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "myCollectionViewCell.h"

@implementation myCollectionViewCell
-(void)layoutSubviews
{
    self.myImageView.frame=CGRectMake(0, 0, self.contentView.frame.size.width,80);
    [self.contentView addSubview:self.myImageView];
    
    self.nameLabel.frame = CGRectMake(0,80 , self.contentView.frame.size.width, 40);
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel setBackgroundColor:[UIColor cyanColor]];

}
@end

 2.實例化UICollectionView,並實現它的代理方法代理

//
//  ViewController.m
//  UICollectionView
//
//  Created by City--Online on 15/5/25.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "ViewController.h"
#import "myCollectionViewCell.h"

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong) UICollectionView *collectionView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //UICollectionViewLayout能夠說是UICollectionView的大腦和中樞,它負責了將各個cell、Supplementary View和Decoration Views進行組織,爲它們設定各自的屬性
    //Layout決定了UICollectionView是如何顯示在界面上的。在展現以前,通常須要生成合適的UICollectionViewLayout子類對象,並將其賦予CollectionView的collectionViewLayout屬性
    UICollectionViewFlowLayout  *collectionViewFlowLayout=[[UICollectionViewFlowLayout alloc]init];
    collectionViewFlowLayout.minimumInteritemSpacing=0.0;
    collectionViewFlowLayout.minimumLineSpacing=0.0;
    collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(0.0, 0.0, 0, 0.0);
    //它定義了每個item的大小。經過設定itemSize能夠全局地改變全部cell的尺寸,若是想要對某個cell制定尺寸,能夠使用-collectionView:layout:sizeForItemAtIndexPath:方法
    collectionViewFlowLayout.itemSize=CGSizeMake(120, 120);
    collectionViewFlowLayout.estimatedItemSize=CGSizeMake(120, 120);
    //由屬性scrollDirection肯定scroll view的方向,將影響Flow Layout的基本方向和由header及footer肯定的section之間的寬度
    collectionViewFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
    //Header和Footer尺寸 一樣地分爲全局和部分。須要注意根據滾動方向不一樣,header和footer的高和寬中只有一個會起做用。垂直滾動時section間寬度爲該尺寸的高,而水平滾動時爲寬度起做用
    collectionViewFlowLayout.headerReferenceSize=CGSizeMake(100, 40);
    collectionViewFlowLayout.footerReferenceSize=CGSizeMake(100, 40);
    
    _collectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:collectionViewFlowLayout];
    _collectionView.backgroundColor=[UIColor whiteColor];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    _collectionView.allowsSelection=YES;
    _collectionView.allowsMultipleSelection=YES;
    
    // 註冊相關類
    [_collectionView registerClass:[myCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];
    //也能夠用Nib
//    [[_collectionView registerNib:<#(UINib *)#> forCellWithReuseIdentifier:<#(NSString *)#>]
//    _collectionView registerNib:<#(UINib *)#> forSupplementaryViewOfKind:<#(NSString *)#> withReuseIdentifier:<#(NSString *)#>
    [self.view addSubview:_collectionView];
    
}

//UICollectionViewDataSource  代理
//每節單元格數
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section==0) {
        return 6;
    }
    else
    {
         return 4;
    }
   
}

//節數
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
//單元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    myCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/ 255.0  green:arc4random()%256 / 255.0 blue:arc4random()% 256 / 255.0  alpha:1];
    cell.myImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];
    cell.nameLabel=[[UILabel alloc]init];
    cell.nameLabel.text=[NSString stringWithFormat:@"%ld %ld",indexPath.section,indexPath.row];
    return cell;
    
}
//節頭節尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString: UICollectionElementKindSectionFooter]) {
        UICollectionReusableView *footer=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Footer" forIndexPath:indexPath];
        footer.backgroundColor=[UIColor yellowColor];
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
        label.text=@"label";
        label.textAlignment=NSTextAlignmentCenter;
        [footer addSubview:label];
        return footer;
    }
    else
    {
        UICollectionReusableView *Header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
        Header.backgroundColor=[UIColor blueColor];
        return Header;
        
    }
}
//UICollectionViewDelegate 代理
//選中時是否高亮
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row%2==0) {
        return YES;
    }
    return NO;
}
//選中高亮顯示後
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didHighlightItemAtIndexPath");
}
// 容許被選中
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//容許被取消
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//選中某個單元格
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@"didSelectItemAtIndexPath section=%ld row=%ld",indexPath.section,indexPath.row);
}
//取消某個單元格
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didDeselectItemAtIndexPath section=%ld row=%ld",indexPath.section,indexPath.row);
}
//高亮不顯示
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSLog(@"didUnhighlightItemAtIndexPath");
}
//即將展現UICollectionViewCell
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"willDisplayCell");
}
//即將顯示section的footer和header
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@"willDisplaySupplementaryView");
}
//UICollectionViewCell顯示完成
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didEndDisplayingCell");
}
//顯示section的footer和header完成
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@"didEndDisplayingSupplementaryView");
}
//是否顯示 copy、cut、paste等
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//是否顯示action輔助功能
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    SEL sel=@selector(copy:);
    if (sel==action) {
        return YES;
    }
    return NO;
}
//判斷選擇的是什麼action
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    SEL sel=@selector(copy:);
    if (sel==action) {
        NSLog(@"aaaaa");
    }
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 在上面的Header、Footer沒有自定義,其實也能夠自定義。UICollectionView中的一些方法和UITableView中的相似,這裏就不舉例了。orm

相關文章
相關標籤/搜索