iOS UICollectionView簡單使用

首先認識一下UICollectionView

NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionView : UIScrollView  

 

UICollectionView 和 UICollectionViewController 類是iOS6 新引進的API,用於展現集合視圖,佈局更加靈活,可實現多列布局,用法相似於UITableView 和 UITableViewController 類。html

使用UICollectionView 必須實現UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout這三個協議。ios

 

下面給出一些經常使用方法,具體的使用能夠參考Demo:點我下載  蘋果官方Demo:點我下載

- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    self.title = @"UICollectionView學習";  
      
    //經過Nib生成cell,而後註冊 Nib的view須要繼承 UICollectionViewCell  
    [self.collectionView registerNib:[UINib nibWithNibName:@"SQCollectionCell" bundle:nil] forCellWithReuseIdentifier:kcellIdentifier];  
      
    //註冊headerView Nib的view須要繼承UICollectionReusableView  
    [self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier];  
    //註冊footerView Nib的view須要繼承UICollectionReusableView  
    [self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kfooterIdentifier];  
    //  
    self.collectionView.allowsMultipleSelection = YES;//默認爲NO,是否能夠多選  
      
}  
  
- (void)didReceiveMemoryWarning  
{  
    [super didReceiveMemoryWarning];  
    // Dispose of any resources that can be recreated.  
}  
#pragma mark -CollectionView datasource  
//section  
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView  
{  
    return 2;  
}  
//item個數  
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section  
{  
    return 6;  
      
}  
  
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:  
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath  
{  
    //重用cell  
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kcellIdentifier forIndexPath:indexPath];  
    //賦值  
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];  
    UILabel *label = (UILabel *)[cell viewWithTag:2];  
    NSString *imageName = [NSString stringWithFormat:@"%ld.JPG",(long)indexPath.row];  
    imageView.image = [UIImage imageNamed:imageName];  
    label.text = imageName;  
      
    cell.backgroundColor = [UIColor redColor];  
    return cell;  
      
}  
// 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{  
      
    NSString *reuseIdentifier;  
    if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){  
        reuseIdentifier = kfooterIdentifier;  
    }else{  
        reuseIdentifier = kheaderIdentifier;  
    }  
      
    UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];  
      
    UILabel *label = (UILabel *)[view viewWithTag:1];  
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]){  
        label.text = [NSString stringWithFormat:@"這是header:%d",indexPath.section];  
    }  
    else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){  
        view.backgroundColor = [UIColor lightGrayColor];  
        label.text = [NSString stringWithFormat:@"這是footer:%d",indexPath.section];  
    }  
    return view;  
}  
//定義每一個UICollectionViewCell 的大小  
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath  
{  
    return CGSizeMake(60, 80);  
}  
//定義每一個Section 的 margin  
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section  
{  
    return UIEdgeInsetsMake(15, 15, 5, 15);//分別爲上、左、下、右  
}  
//返回頭headerView的大小  
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{  
    CGSize size={320,45};  
    return size;  
}  
//返回頭footerView的大小  
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section  
{  
    CGSize size={320,45};  
    return size;  
}  
//每一個section中不一樣的行之間的行間距  
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section  
{  
    return 10;  
}  
//每一個item之間的間距  
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section  
//{  
//    return 100;  
//}  
//選擇了某個cell  
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  
{  
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];  
    [cell setBackgroundColor:[UIColor greenColor]];  
}  
//取消選擇了某個cell  
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath  
{  
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];  
    [cell setBackgroundColor:[UIColor redColor]];  
}  

 

效果圖以下:app

相關文章
相關標籤/搜索