Use of ios UICollectionView

Source: Internet
Author: User

Use of ios UICollectionView

There are two methods to use UICollectionView: one is to inherit UICollectionViewController, which will bring a UICollectionView; the other is to place a view in a common UIViewController.

I prefer the second type. The second method is used to briefly introduce the use of UICollectionView.

1. UIViewController implements delegation. The Code is as follows:

@ Interface YourViewController: UIViewController

2. Declare UICollectionView. The Code is as follows:

@ Property (nonatomic, retain) UICollectionView * myCollectionView;

3. initialize UICollectionView. The Code is as follows:

-(Void) viewDidLoad
{
[Super viewDidLoad];
[Self initConllectionView];
}
-(Void) initConllectionView {
CircleLayout * layout = [[CircleLayout alloc] init];
MyCollectionView = [[UICollectionView alloc] initWithFrame: self. view. bounds collectionViewLayout: layout];
[MyCollectionView registerClass: [UICollectionViewCell class] forCellWithReuseIdentifier: @ "cell"];
MyCollectionView. backgroundColor = [UIColor whiteColor];
MyCollectionView. delegate = self;
MyCollectionView. dataSource = self;
[Self. view addSubview: myCollectionView];
[Layout release];
}

The CircleLayout inherits from UICollectionViewLayout, which is used to display the layout of UICollectionView and some attributes.

4. Implement-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath;

-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath {
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier: CELL_STR forIndexPath: indexPath];
For (UIView * view in cell. contentView. subviews ){
If (view ){
[View removeFromSuperview];
}
}
UIImageView * imgView = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0, ITEM_WIDTH, ITEM_WIDTH)];
If (indexPath. row> 4 ){
ImgView. image = [UIImage imageNamed: @ "apple.png"];
} Else {
ImgView. image = [UIImage imageNamed: @ "applec.png"];
}
[Cell. contentView addSubview: imgView];
[ImgView release];
Return cell;
}

5. cell Size

-(CGSize) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout sizeForItemAtIndexPath :( NSIndexPath *) indexPath {
Return CGSizeMake (130,130 );
}

 

CircleLayout code:

CircleLayout. h

# Import
# Import
@ Interface CircleLayout: UICollectionViewLayout
@ Property (nonatomic, assign) CGPoint center;
@ Property (nonatomic, assign) CGFloat radius;
@ Property (nonatomic, assign) NSInteger cellCount;
@ End

 

CircleLayout. m

 

# Define ITEM_SIZE 130
# Import "CircleLayout. h"

@ Implementation CircleLayout
@ Synthesize cellCount, center, radius;
-(Void) prepareLayout {
[Super prepareLayout];
CGSize size = self. collectionView. frame. size;
CellCount = [self. collectionView numberOfItemsInSection: 0];
Center = CGPointMake (size. width/2, size. height/2 );
Radius = minute (size. width, size. height)/2.5;
}
-(CGSize) collectionViewContentSize {
Return [self collectionView]. frame. size;
}
-(UICollectionViewLayoutAttributes *) layoutAttributesForItemAtIndexPath :( NSIndexPath *) path
{
UICollectionViewLayoutAttributes * attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath: path];
Attributes. size = CGSizeMake (ITEM_SIZE, ITEM_SIZE );
Attributes. center = CGPointMake (center. x + radius * cosf (2 * path. item * M_PI/cellCount ),

Center. y + radius * sinf (2 * path. item * M_PI/cellCount ));
Return attributes;
}
-(NSArray *) layoutAttributesForElementsInRect :( CGRect) rect {
NSMutableArray * attributes = [NSMutableArray array];
For (NSInteger I = 0; I <self. cellCount; I ++ ){
NSIndexPath * indexPath = [NSIndexPath indexPathForItem: I inSection: 0];
[Attributes addObject: [self layoutAttributesForItemAtIndexPath: indexPath];
}
Return attributes;
}
-(UICollectionViewLayoutAttributes *) initialLayoutAttributesForInsertedItemAtIndexPath :( NSIndexPath *) itemIndexPath {
UICollectionViewLayoutAttributes * attributes = [self layoutAttributesForItemAtIndexPath: itemIndexPath];
Attributes. alpha = 0.0;
Attributes. center = CGPointMake (center. x, center. y );
Return attributes;
}
-(UICollectionViewLayoutAttributes *) finalLayoutAttributesForDeletedItemAtIndexPath :( NSIndexPath *) itemIndexPath
{
UICollectionViewLayoutAttributes * attributes = [self layoutAttributesForItemAtIndexPath: itemIndexPath];
Attributes. alpha = 0.0;
Attributes. center = CGPointMake (center. x, center. y );
Attributes. transform3D = CATransform3DMakeScale (0.1, 0.1, 1.0 );
Return attributes;
}
@ End


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.