IOS-UICollectionView (original) and iosuicollectionview
Preface
UICollectionView is a new data display method. In short, it can be understood as a UITableView with multiple columns (please note that this is the simplest form of UICollectionView ). If you have used iBooks, you may still have a certain impression on the shelf layout: there are various types of books you download and purchase on a virtual shelf, neatly arranged. In fact, this is a form of UICollectionView, or various clocks in the iOS 6 native clock application of the iPad, which is also the simplest layout of UICollectionView.
Basic knowledge 1. common methods for creating UICollectionView
1. Create cell and header, footer
Create with code
-RegisterClass: forCellWithReuseIdentifier:-registerClass: forSupplementaryViewOfKind: withReuseIdentifier:
Use xib to create-registerNib: forCellWithReuseIdentifier:-registerNib: forSupplementaryViewOfKind: withReuseIdentifier:
Reuse cell-dequeueReusableCellWithReuseIdentifier: forIndexPath:-dequeueReusableSupplementaryViewOfKind: withReuseIdentifier: forIndexPath:
2. Obtain the Item and position in the Collection View-indexPathForItemAtPoint:-indexPathsForVisibleItems-indexPathForCell:-cellForItemAtIndexPath:
3. obtain the status of the Collection View-numberOfSections-numberOfItemsInSection:
Ii. Proxy method 1. UICollectionViewDelegate
1. process the selected Cells
-CollectionView: shouldSelectItemAtIndexPath:
-CollectionView: didSelectItemAtIndexPath:
-CollectionView: shouldDeselectItemAtIndexPath:
-CollectionView: didDeselectItemAtIndexPath:
2. Process cell highlighting
-CollectionView: shouldHighlightItemAtIndexPath:
-CollectionView: didHighlightItemAtIndexPath:
-CollectionView: didUnhighlightItemAtIndexPath:
2. UICollectionViewDataSource
1. Get the number of sections and items
-CollectionView: numberOfItemsInSection:
-NumberOfSectionsInCollectionView:
2. Get the Items view
-CollectionView: cellForItemAtIndexPath:
-CollectionView: viewForSupplementaryElementOfKind: atIndexPath:
To learn UICollectionView, we can compare it with UITableView. Here we will write a simple example to illustrate the effects of these methods.
1. initialize the view layout object import proxy. Here we will talk about UICollectionView. There are three proxy methods available for importing according to the actual situation. <1> UICollectionViewDataSource <2> UICollectionViewDelegate <3> UICollectionViewDelegateFlowLayout
-(Void) viewDidLoad {[super viewDidLoad]; // initialize a view layout object UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init]; UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame: self. view. frame collectionViewLayout: flowLayout]; collectionView. backgroundColor = [UIColor whiteColor]; collectionView. dataSource = self; collectionView. delegate = self; [self. view addSubview: collectionView]; // to register a cell, you must have [collectionView registerClass: [UICollectionViewCell class] forCellWithReuseIdentifier: @ "UICollectionViewCell"];}
2. Implement specific proxy methods. The following code has detailed comments.
// Define the number of uicollectionviewcells-(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section {return 20;} // define the number of display sections-(NSInteger) numberOfSectionsInCollectionView :( UICollectionView *) collectionView {return 2;} // cell-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath {static NSString * CellID = @ "UICollectionViewCell"; UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier: cellID forIndexPath: indexPath]; cell. backgroundColor = [UIColor orangeColor]; return cell;} // defines the size of each item-(CGSize) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout layout :( NSIndexPath *) indexPath {return CGSizeMa Ke (50, 50);} // distance between the upper left and lower right groups-(UIEdgeInsets) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout insetForSectionAtIndex :( NSInteger) section {// return UIEdgeInsetsMake (50, 50, 50, 50);} // method called when UICollectionView is selected-(void) collectionView :( UICollectionView *) collectionView didSelectItemAtIndexPath :( NSIndexPath *) indexPath {UICollectionViewCell * cel L = (UICollectionViewCell *) [collectionView cellForItemAtIndexPath: indexPath]; // temporarily change the color, optimistic, but only temporarily changed. If you want to change the data source permanently, you can change the data source and control it in cellForItemAtIndexPath. (Similar to UITableView! O (worker _ worker) O ~) Cell. backgroundColor = [UIColor redColor]; NSLog (@ "item =======% ld", (long) indexPath. item); NSLog (@ "row = % ld", indexPath. row); NSLog (@ "section = % ld", indexPath. section);} // click cell to operate on the previous cell // deselect the selected operation-(void) collectionView :( UICollectionView *) collectionView didDeselectItemAtIndexPath :( NSIndexPath *) indexPath {UICollectionViewCell * cell = (UICollectionViewCell *) [collectionView cellForItemAtIndexPath: indexPath]; cell. backgroundColor = [UIColor yellowColor];} // returns whether this UICollectionView can be selected-(BOOL) collectionView :( UICollectionView *) collectionView shouldSelectItemAtIndexPath :( NSIndexPath *) indexPath {return YES ;} // set the minimum row spacing, that is, the minimum interval between the first row and the next row-(CGFloat) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout minimumLineSpacingForSectionAtIndex :( NS) section {return 10;} // set the minimum column spacing, that is, the minimum interval between the left and right rows-(CGFloat) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout minimumInteritemSpacingForSectionAtIndex :( NSInteger) section {return 10;} // you can specify whether to uncheck-(BOOL) collectionView (UICollectionView *) collectionView shouldDeselectItemAtIndexPath :( NSIndexPath *) indexPath {NSLog (@ "% s", _ FUNCTION _); return YES;} // callback when highlighted is converted to non-highlighted-(void) collectionView :( UICollectionView *) collectionView didUnhighlightItemAtIndexPath :( NSIndexPath *) indexPath {NSLog (@ "% s", _ FUNCTION _);} // callback after highlighted-(void) collectionView :( UICollectionView *) collectionView didHighlightItemAtIndexPath :( NSIndexPath *) indexPath {NSLog (@ "% s", _ FUNCTION _);} // highlight (BOOL) when the selected value is allowed) collectionView :( UICollectionView *) collectionView shouldHighlightItemAtIndexPath :( NSIndexPath *) indexPath {NSLog (@ "% s", _ FUNCTION _); return YES ;}
To sum up, these proxy methods can be selected based on the actual situation in development. Of course, custom cells like UITableView only need to be inherited to UICollectionViewCell.