IOS Uicollectionview Simple to use

Source: Internet
Author: User
Tags vars

First, meet Uicollectionview. [OBJC]View Plaincopy
    1. Ns_class_available_ios (6_0) @interface uicollectionview:uiscrollview

The Uicollectionview and Uicollectionviewcontroller classes are IOS6 's newly introduced APIs for presenting collection views, with a more flexible layout that enables multi-column layouts, similar to UITableView and Uitableviewcontroller class.

Using Uicollectionview must implement Uicollectionviewdatasource,uicollectionviewdelegate, Uicollectionviewdelegateflowlayout these three agreements.

Here are some common methods, specific use can refer to the demo: point I download Apple official demo: click I download [OBJC]View Plaincopy
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. self. title = @ "Uicollectionview learning";
  5. //Generate cell via NIB, and then register the nib's view to inherit Uicollectionviewcell
  6. [self. CollectionView registernib:[uinib nibwithnibname:@ "Sqcollectioncell" bundle:  Nil] Forcellwithreuseidentifier:kcellidentifier];
  7. //Register Headerview NIB's view needs to inherit Uicollectionreusableview
  8. [self. CollectionView registernib:[uinib nibwithnibname:@ "Sqsupplementaryview" bundle: Nil] Forsupplementaryviewofkind:uicollectionelementkindsectionheader withreuseidentifier:  Kheaderidentifier];
  9. //Register Footerview NIB's view needs to inherit Uicollectionreusableview
  10. [self. CollectionView registernib:[uinib nibwithnibname:@ "Sqsupplementaryview" bundle: Nil] forsupplementaryviewofkind:uicollectionelementkindsectionfooter withreuseidentifier:  Kfooteridentifier];
  11. //  
  12. self. CollectionView. allowsmultipleselection = YES; The default is no, whether you can select multiple
  13. }
  14. -(void) didreceivememorywarning
  15. {
  16. [Super didreceivememorywarning];
  17. //Dispose of any resources, can be recreated.
  18. }
  19. #pragma mark-collectionview DataSource
  20. Section
  21. -(Nsinteger) Numberofsectionsincollectionview: (uicollectionview *) CollectionView
  22. {
  23. return 2;
  24. }
  25. Item Count
  26. -(Nsinteger) CollectionView: (uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
  27. {
  28. return 6;
  29. }
  30. The cell is returned must was retrieved from a call To-dequeuereusablecellwithreuseidentifier:forindexpath:
  31. -(Uicollectionviewcell *) CollectionView: (uicollectionview *) CollectionView Cellforitematindexpath: ( Nsindexpath *) indexpath
  32. {
  33. //Reuse cell
  34. Uicollectionviewcell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:kcellidentifier   Forindexpath:indexpath];
  35. //Assigned value
  36. Uiimageview *imageview = (Uiimageview *) [cell Viewwithtag:1];
  37. UILabel *label = (UILabel *) [cell Viewwithtag:2];
  38. nsstring *imagename = [NSString stringwithformat:@ "%ld.  JPG ", (long) Indexpath. row];
  39. ImageView. image = [UIImage imagenamed:imagename];
  40. Label. Text = ImageName;
  41. Cell. backgroundcolor = [Uicolor redcolor];
  42. return cell;
  43. }
  44. The view that is returned must was retrieved from a call To-dequeuereusablesupplementaryviewofkind:withreuseidentifier: Forindexpath:
  45. -(Uicollectionreusableview *) CollectionView: (uicollectionview *) CollectionView Viewforsupplementaryelementofkind: (nsstring *) Kind atindexpath: (nsindexpath *) indexpath{
  46. NSString *reuseidentifier;
  47. if ([Kind isequaltostring:uicollectionelementkindsectionfooter]) {
  48. Reuseidentifier = Kfooteridentifier;
  49. }else{
  50. Reuseidentifier = Kheaderidentifier;
  51. }
  52. Uicollectionreusableview *view = [CollectionView dequeuereusablesupplementaryviewofkind:kind   Withreuseidentifier:reuseidentifier Forindexpath:indexpath];
  53. UILabel *label = (UILabel *) [view Viewwithtag:1];
  54. if ([Kind Isequaltostring:uicollectionelementkindsectionheader]) {
  55. Label. Text = [NSString stringwithformat:@ "This is header:%d", Indexpath. section];
  56. }
  57. Else if ([Kind isequaltostring:uicollectionelementkindsectionfooter]) {
  58. View. backgroundcolor = [Uicolor lightgraycolor];
  59. Label. Text = [NSString stringwithformat:@ "This is footer:%d", Indexpath. section];
  60. }
  61. return view;
  62. }
  63. Define the size of each Uicollectionviewcell
  64. -(Cgsize) CollectionView: (uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Sizeforitematindexpath: (nsindexpath *) indexpath
  65. {
  66. return Cgsizemake (60, 80);
  67. }
  68. Define margin for each section
  69. -(Uiedgeinsets) CollectionView: (uicollectionview *) CollectionView layout: (uicollectionviewlayout *) Collectionviewlayout Insetforsectionatindex: (nsinteger) section
  70. {
  71. return Uiedgeinsetsmake (15, 15, 5, 15); Top, left, bottom, right, respectively
  72. }
  73. Returns the size of the head Headerview
  74. -(Cgsize) CollectionView: (uicollectionview *) CollectionView layout: (uicollectionviewlayout *) Collectionviewlayout referencesizeforheaderinsection: (nsinteger) section{
  75. Cgsize size={320,45};
  76. return size;
  77. }
  78. Returns the size of the head Footerview
  79. -(Cgsize) CollectionView: (uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout referencesizeforfooterinsection: (nsinteger) section
  80. {
  81. Cgsize size={320,45};
  82. return size;
  83. }
  84. Line spacing between different rows in each section
  85. -(CGFloat) CollectionView: (uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Minimumlinespacingforsectionatindex: (nsinteger) section
  86. {
  87. return 10;
  88. }
  89. The spacing between each item
  90. -(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Minimuminteritemspacingforsectionatindex: (nsinteger) Section
  91. //{
  92. return 100;
  93. //}
  94. A cell was selected
  95. -(void) CollectionView: (uicollectionview *) CollectionView didselectitematindexpath: (Nsindexpath *) Indexpath
  96. {
  97. Uicollectionviewcell *cell = [CollectionView Cellforitematindexpath:indexpath];
  98. [Cell Setbackgroundcolor:[uicolor Greencolor]];
  99. }
  100. Deselect a cell
  101. -(void) CollectionView: (uicollectionview *) CollectionView diddeselectitematindexpath: (Nsindexpath *) Indexpath
  102. {
  103. Uicollectionviewcell *cell = [CollectionView Cellforitematindexpath:indexpath];
  104. [Cell Setbackgroundcolor:[uicolor Redcolor]];
  105. }

As follows: Original link: http://blog.csdn.net/Apple_app/article/details/38867123

IOS Uicollectionview Simple to use

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.