This chapter first introduces UICollectionView and its common methods, and then combines an instance to learn how to use UICollectionView.
The UICollectionView and UICollectionViewController classes are newly introduced APIS of iOS6. They are used to display the set view. The layout is more flexible and can implement multi-column layout. The usage is similar to the UITableView and UITableViewController classes.
To use UICollectionView, you must implement the following protocols: UICollectionViewDataSource, UICollectionViewDelegate, and UICollectionViewDelegateFlowLayout.
The following describes some common methods. (Only common APIs are provided. Other APIs can be viewed)
- # Pragma mark -- UICollectionViewDataSource
- // Define the number of uicollectionviewcells displayed
- -(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section
- {
- Return 30;
- }
- // Define the number of displayed sections
- -(NSInteger) numberOfSectionsInCollectionView :( UICollectionView *) collectionView
- {
- Return 1;
- }
- // Content displayed for each UICollectionView
- -(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath
- {
- Static NSString * CellIdentifier = @ "GradientCell ";
- UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath: indexPath];
- Cell. backgroundColor = [UIColor colorWithRed :( (10 * indexPath. row)/255.0) green :( (20 * indexPath. row)/255.0) blue :( (30 * indexPath. (row)/255.0) alpha: 1.0f];
- Return cell;
- }
- # Pragma mark -- UICollectionViewDelegateFlowLayout
- // Define the size of each UICollectionView
- -(CGSize) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout sizeForItemAtIndexPath :( NSIndexPath *) indexPath
- {
- Return CGSizeMake (96,100 );
- }
- // Define the margin of each UICollectionView
- -(UIEdgeInsets) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout insetForSectionAtIndex :( NSInteger) section
- {
- Return UIEdgeInsetsMake (5, 5, 5, 5 );
- }
- # Pragma mark -- UICollectionViewDelegate
- // Method called when UICollectionView is selected
- -(Void) collectionView :( UICollectionView *) collectionView didSelectItemAtIndexPath :( NSIndexPath *) indexPath
- {
- UICollectionViewCell * cell = (UICollectionViewCell *) [collectionView cellForItemAtIndexPath: indexPath];
- Cell. backgroundColor = [UIColor whiteColor];
- }
- // Return whether the UICollectionView can be selected
- -(BOOL) collectionView :( UICollectionView *) collectionView shouldSelectItemAtIndexPath :( NSIndexPath *) indexPath
- {
- Return YES;
- }
The following is an example. (The example is from the network. However, it is obtained through a third party and cannot be connected. Sorry .)
The emergence of iOS CollectionView is a major benefit. You no longer need to use TableView to define complex multi-column tables. The usage is similar to that of Table, but the Cell must be added by yourself, with no default mode.
Because CollectionView does not have the default Cell Layout, it is generally convenient and convenient to customize.
1. Custom Cell
1. New Class CollectionCell inherited from UICollectionViewCell
2. Create Xib and name it CollectionCell. xib.
A. Select CollectionCell. xib to delete the default View, drag a Collection View Cell (Figure 3) from the control to the canvas, and set the size to 95*116;
B. Select the Cell you just added and change the class name to CollectionCell, 4
C. Add an ImageView and a Label to CollectionCell of CollectionCell. xib (figure 5)
D. Create a ing, as shown in Figure 6 and Figure 7.
E. Select CollectionCell. m and rewrite the init method.
- -(Id) initWithFrame :( CGRect) frame
- {
- Self = [super initWithFrame: frame];
- If (self)
- {
- // Load the collectionCell. xib file during initialization
- NSArray * arrayOfViews = [[NSBundle mainBundle] loadNibNamed: @ "CollectionCell" owner: self options: nil];
- // Return nil if the path does not exist
- If (arrayOfViews. count <1)
- {
- Return nil;
- }
- // If the view in xib does not belong to the UICollectionViewCell class, return nil
- If (! [[ArrayOfViews objectAtIndex: 0] isKindOfClass: [UICollectionViewCell class])
- {
- Return nil;
- }
- // Load nib
- Self = [arrayOfViews objectAtIndex: 0];
- }
- Return self;
- }
F. Select CollectionCell. xib and change its identifier to CollectionCell.
Ii. Define UICollectionView;
1. Drag a Collection View to the View of the specified ViewController.
2. Connect dataSource and delegate, create a ing, and name it CollectionView.
3. Select the scale of CollectionView and change the Width and Height of the Cell Size to 95*116, which is the same as that of the custom Cell, as shown in figure 8.
4. Select the CollectionView attribute to modify its attributes, such as Vertical sliding or Horizontal sliding. Select Vertical or Horizontal.
5. Select CollectionViewCell, modify Class, and inherit from CollectionCell
5. Declare the Cell class in the ViewDidLoad method and add it to the ViewDidLoad method. If this sentence is not declared, it cannot be loaded and the program crashes.
CollectionCell is the Cell identifier (defined in the previous steps. )
- [Self. collectionView registerClass: [CollectionCell class] forCellWithReuseIdentifier: @ "CollectionCell"];
6. Declare the proxy in ViewController. h.
- @ Interface ViewController: UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>
7. modern management methods in. m Files
- // Number of items in each section
- -(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section
- {
- Return 12;
- }
- -(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath
- {
- CollectionCell * cell = (CollectionCell *) [collectionView dequeueReusableCellWithReuseIdentifier: @ "CollectionCell" forIndexPath: indexPath];
- // Image name
- NSString * imageToLoad = [NSString stringWithFormat: @ "mongod.png", indexPath. row];
- // Load the image
- Cell. imageView. image = [UIImage imageNamed: imageToLoad];
- // Set the label text
- Cell. label. text = [NSString stringWithFormat: @ "{% ld, % ld}", (long) indexPath. row, (long) indexPath. section];
- Return cell;
- }
8. Effect 10
After clicking an item, the jump event is similar to the UITableView to implement the proxy method.
- -(Void) collectionView :( UICollectionView *) collectionView didSelectItemAtIndexPath :( NSIndexPath *) indexPath
No more