Wednesday, 12 April 2017

About UICollectionView in IOS

UICollectionView, Manages an ordered collection of data item and present them using customizable layouts.
UICollectionView is same as the UITableView. UITableView manages a collection of data or information and display then on the screen in a single column layout whereas UICollectionView offers developers flexibility to present item using customize layout.
UICollectionView has three components:-
  1. Cell:
It is an instance of UICollectionViewCell . It represent a single item in the data collection.
  1. Supplementary views:
It is optional and usually used to implement the header or footer view of the sections.
  1. Decoration views:
It is another type of supplementary view but for decoration purpose only. It is not related to data collection. We create decoration view to enhance the visual appearance of the CollectionView.
Step 1:
Open the Excode and select the Single View Application.

Step 2:
Create project with “ExCollectionView” name.

Step 3:
Open the StoryBoard and there is a ViewController and drag the CollectionVIew inside the ViewController .
And set the Add New Constraints.

Step 4:
Select now the cell inside the UIViewController and add the UIImage in the UICollectionView cell and also set the UIImage constraints.

And open the ViewController.swift and add the extension of  UICollectionViewDataSource,UICollectionViewDelegatein the ViewCollection.swift.
And these three method in the ViewController.swift:
func numberOfSections(in collectionView: UICollectionView) -> Int {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) ->      UICollectionViewCell {

After that select the UICollectionViewCell and set the name of the reuseIdentifier = “MyCollectionViewCell”
Using Assistant Editor, add an IBOutlet to CollectionView in ViewController.swift
@IBOutlet weak var collectionView: UICollectionView!
Create a new Cocoa Touch Class file (File > New > File… > iOS > Cocoa Touch Class). Name it MyCollectionViewCell. This class will hold the outlets for the views that you add to your cell in the storyboard.

And open the ViewController.swift and add this code in the numberOfItemInSection in collectionview:

And go to the main story board and select the UIImage and set the image in the UIImage.

And also add one method in the viewcontroller.swift:
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout:    UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return (indexPath.section == 0) ? CGSize(width: collectionView.bounds.size.width, height: 200) : CGSize(width: collectionView.bounds.size.width/2 – 2, height: 170)
And after the execude your code

It should be look like this. If you want to add multi image in the UIImageView then you use the array and pass the image in the array and cell in the cellForRowAtIndexPath method.

No comments:

Post a Comment