IOS: Create a subclass of UICollectionView
The creation of UICollectionView is basically the same as that of UITableView.
First, create a subclass that inherits from UICollectionView
Then set some attributes in the initialization method.
-(Id) initWithFrame :( CGRect) frame {UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout. minimumInteritemSpacing = 0; // The column spacing is flowLayout. minimumLineSpacing = 0; // line spacing self = [super initWithFrame: frame collectionViewLayout: flowLayout]; if (self) {// hide the slider self. showsHorizontalScrollIndicator = NO; self. showsVerticalScrollIndicator = NO; // sets the proxy self. delegate = self; self. dataSource = self; // set the background color (black by default) self. backgroundColor = [UIColor whiteColor]; // register the cell [self registerClass: [YSStudentStatusCell class] forCellWithReuseIdentifier: identify];} return self ;}
The Protocol method of collectionView is basically the same as that of tableView. The main difference is that cell creation and header view creation.
First look at cell Creation
// Create a cell
-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath
{
ModelStudent * model = self. dataArray [indexPath. item];
// Subclass cell
YSStudentStatusCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier: identify forIndexPath: indexPath];
[Cell configureWithModel: model. StuUserInfo];
Return cell;
}
The initialization method for cell subclass creation in collectionView is as follows:
- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self initView]; } return self;}
To create a collectionView header view, you must first register the header view. Register a demo. I generally write it in a cell registration module.
// Register the collection header view [self registerClass: [UICollectionReusableView class] forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @ "Identifierhead"];
How to Create a header view Protocol
// Create the group's header view-(UICollectionReusableView *) collectionView :( UICollectionView *) collectionView detail :( NSString *) kind atIndexPath :( NSIndexPath *) indexPath {UICollectionReusableView * headView = [collectionView detail: kind withReuseIdentifier: @ "Identifierhead" forIndexPath: indexPath]; headView. backgroundColor = [UIColor blueColor]; return headView ;}
In addition, the use of flowLayout
FlowLayout is the layout attribute of collectionView. Different flowLayout settings can be used to load different layout patterns of collectionView.