UICollectionView using iOS9 Swift 2.1

Today’s topic is UICollectionView using iOS9 Swift 2.1. We will learn how to use collection view the best example is in build photo app of iPhone. The UICollectionView class manages an ordered collection of data items, we can presents them using customisable layouts. UICollectionViews provide the same function as UITableViews except that a collection view is able to support more than just single-column layouts. Collection views support customisable layouts that can be used to implement multi-column grids, tiled layouts, circular layouts, and many more. We can even change the layout of a collection view dynamically.

So lets start with new project Name it to CollectionDemo.

UICollectionView using Swift iOS
UICollectionView using Swift iOS

Go to Main.storyboard drag and drop a UICollectionView to controller. In the “Size Inspector” you can resize the cell according to your need like here i created a cell of 150*150.

Next, select the Collection View Cell and set the identifier as “cell” in the Attribute Inspector.

UICollectionView using iOS Swift 2.1
UICollectionView using iOS9 Swift 2.1

Create an outlet and connect the delegates and datasource also import them into your ViewController.Swift class.
In Project Navigator, right-click and select “New File”. Create a new class that is a subclass of UICollectionViewCell and name it as “CollectionCell”. Select the cell and change its class to the “CollectionCell”, now drag drop a UILabel make its outlet on the custom cell class.

UICollectionView using iOS Swift 2.1
UICollectionView using iOS9 Swift 2.1

Lets do some coding for our UICollectionView using iOS9 Swift 2.1

2 methods below are must for collection view.

The first method is to define your cell same as table view you can change your cell on run time. Just like tableview the cell will be reused.

Second method is just telling us the number of collection view cell we wanted.

 

Lets access that label and display something on it, like its index etc.

Now build and run your project.
Cool, right? As always, leave us comment and share your thought about the tutorial. Happy Coding 😉

Today we learn about UICollectionView using iOS9 Swift 2.1 next time we will do something more exciting like Playing Audio files.

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.