CollectionView's case

Source: Internet
Author: User

    1. #import "ViewController.h"
    2. #import "CollectionViewCell.h"
    3. @interface Viewcontroller () <uicollectionviewdatasource,uicollectionviewdelegate, Uicollectionviewdelegateflowlayout>
    4. @property (Weak, nonatomic) Iboutlet Uicollectionview *collectioncell;
    5. @end

  1. @implementation Viewcontroller
  2. -(void) viewdidload
  3. {
  4. [Super Viewdidload];
  5. Self.collectionCell.backgroundColor = [Uicolor Greencolor];
  6. Self.collectionCell.dataSource = self;
  7. Self.collectionCell.delegate = self;
  8. }
  9. #pragma mark--Uicollectionviewdatasource
  10. Set the number of Collectioncell
  11. -(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
  12. {
  13. return 9;
  14. }
  15. Define the number of sections to show
  16. -(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView
  17. {
  18. return 1;
  19. }
  20. What each Uicollectionview shows
  21. -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath
  22. {
  23. Registering with Collectioncell, loading views through xib files
  24. [Self.collectioncell registernib:[uinib nibwithnibname:@ "MyCell" Bundle:nil]
  25. forcellwithreuseidentifier:@ "MyCell"];
  26. Collectionviewcell *cell = (Collectionviewcell *) [CollectionView dequeuereusablecellwithreuseidentifier:@ "MyCell" Forindexpath:indexpath];
  27. Picture name
  28. NSString *imagetoload = [NSString stringwithformat:@ "%ld.jpg", Indexpath.row];
  29. Loading pictures
  30. Cell.iamgeView.image = [UIImage imagenamed:imagetoload];
  31. Set Label text
  32. Cell.labeiInfo.text = [NSString stringwithformat:@ "Image%ld", Indexpath.row];
  33. return cell;
  34. }
  35. Set each individual size
  36. -(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) Indexpath
  37. {
  38. Return Cgsizemake (170, 170);
  39. }
  40. Define margin for each Uicollectionview
  41. -(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) Collectionviewlayout Insetforsectionatindex: (nsinteger) Section
  42. {
  43. Return Uiedgeinsetsmake (5, 5, 5, 5);
  44. }
  45. method to invoke when Uicollectionview is selected
  46. -(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) IndexPath
  47. {
  48. Uicollectionviewcell * Cell = (Uicollectionviewcell *) [CollectionView Cellforitematindexpath:indexpath];
  49. Cell.backgroundcolor = [Uicolor Whitecolor];
  50. }
  51. Returns whether this Uicollectionview can be selected
  52. -(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldselectitematindexpath: (Nsindexpath *) IndexPath
  53. {
  54. return YES;
  55. }
  56. Hide the status bar
  57. -(BOOL) Prefersstatusbarhidden
  58. {
  59. return YES;
  60. }
  61. @end

Program Run Result:

CollectionView's case

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.