A set view that can be rolled over and a set view that can be rolled over
This demo adds the set view to the scroll view for slide.
()
1. Create a view and define a set view and a rolling View
1 # import "ViewController. h "2 # import" MyCollectionViewCell. h "// a custom collectionViewCell 3 # define MainWidth [UIScreen mainScreen]. bounds. size. width 4 # define MainHeight [UIScreen mainScreen]. bounds. size. height 5 6 @ interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate> 7 @ property (nonatomic, strong) UICollectionView * collectView; 8 @ property (nonatomic, strong) UIScrollView * scrollView; 9 @ property (nonatomic, strong) UICollectionViewFlowLayout * collectVFL; 10 @ end
2. initialize the set view and scroll view we define
1-(void) viewDidLoad {2 [super viewDidLoad]; 3 // set the bottom scroll effect _ 1 4 self. scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake (0, MainHeight-64-55, MainWidth-55, 55)]; // set the size of the scroll view displayed on the screen 5 self. scrollView. backgroundColor = [UIColor clearColor]; 6 self. scrollView. bounces = NO; 7 self. scrollView. contentSize = CGSizeMake (MainWidth-55)/3*5, 55); // The total size of the scroll view 8 [self. view addSubview: self. scrollView]; 9 10 // set the bottom scroll effect _ 211 // UICollectionViewFlowLayout is the layout of the set view, which inherits from UICollectionViewLayout (the related attributes are not listed one by one, please refer to the official documentation) 12 UICollectionViewFlowLayout * collectVFL = [[UICollectionViewFlowLayout alloc] init]; 13 collectVFL. itemSize = CGSizeMake (MainWidth-55)/3, 55); 14 collectVFL. minimumLineSpacing = 0; 15 collectVFL. minimumInteritemSpacing = 0; 16 collectVFL. scrollDirection = uicollectionviewscrolldirehorizontal; 17 self. collectVFL = collectVFL; 18 19 UICollectionView * collectView = [[UICollectionView alloc] initWithFrame: CGRectMake (, (MainWidth-55)/3*5, 55) collectionViewLayout: collectVFL]; 20 self. collectView = collectView; 21 collectView. backgroundColor = [UIColor whiteColor]; 22 collectView. delegate = self; 23 collectView. dataSource = self; 24 collectView. scrollEnabled = NO; 25 // Note: After the collection view is initialized, you must register 26 [collectView registerClass: [MyCollectionViewCell class] forCellWithReuseIdentifier: @ "cell"]; 27 // Add the set view to the rolling view 28 [self. scrollView addSubview: collectView]; 29 30 [self. view setBackgroundColor: [UIColor grayColor]; 31 32}
3. Proxy method for implementing the set view
1 # pragma mark-set view proxy 2-(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section {3 return 5; 4} 5 6-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath {7 8 static NSString * ide = @ "cell"; 9 MyCollectionViewCell * cell = [collectionView layout: ide forIndexPath: indexPath]; 10 11 cell. titleLabel. text = @ "HAHAHA"; 12 cell. backgroundColor = [UIColor clearColor]; 13 cell. photoImage. image = [UIImage imageNamed: @ "running status @ 2x"]; 14 return cell; 15} 16 17-(void) collectionView :( UICollectionView *) collectionView didSelectItemAtIndexPath :( NSIndexPath *) indexPath {18 NSLog (@ "clicked Item: % ld", indexPath. item); 19}
4. End! This is the first time that a small editor wrote a blog. If you have any shortcomings, please give them some advice.