How To Use UICollectionView In Swift Ios
=> Here Given One of Example UIColletionView:
// AlbumViewController.swift
// UICollectionView+Swift
import UIKit
let reuseIdentifier = "Cell"
class AlbumViewController: UICollectionViewController {
var Albums = Array<String>()
@IBAction func EditAlbumPressed(sender : AnyObject) {
if(self.navigationItem.rightBarButtonItem?.title == "Edit"){
self.navigationItem.rightBarButtonItem?.title = "Done"
for item in self.collectionView!.visibleCells() as [AlbumCell] {
var indexpath : NSIndexPath = self.collectionView!.indexPathForCell(item as AlbumCell)!
var cell : AlbumCell = self.collectionView!.cellForItemAtIndexPath(indexpath) as AlbumCell
//Profile Picture
//var img : UIImageView = cell.viewWithTag(100) as UIImageView
//img.image = UIImage(named: "q.png") as UIImage
//Close Button
var close : UIButton = cell.viewWithTag(102) as UIButton
close.hidden = false
}
} else {
self.navigationItem.rightBarButtonItem?.title = "Edit"
self.collectionView?.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
Albums = ["a.png", "b.png", "c.png", "d.png", "e.png", "f.png", "g.png", "h.png",
"i.png", "j.png", "k.png", "l.png", "m.png"]
}
// #pragma mark UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView?) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView?,
numberOfItemsInSection section: Int) -> Int {
return Albums.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:
NSIndexPath) -> UICollectionViewCell {
/*
We can use multiple way to create a UICollectionViewCell.
*/
//1.
//We can use Reusablecell identifier with custom UICollectionViewCell
/*
let cell = collectionView!.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
as UICollectionViewCell
var AlbumImage : UIImageView = cell.viewWithTag(100) as UIImageView
AlbumImage.image = UIImage(named: Albums[indexPath.row])
*/
//2.
//here Created a Class file for UICollectionViewCell
let cell : AlbumCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",
forIndexPath: indexPath) as AlbumCell
cell.backgroundView = UIImageView(image: UIImage(named: "photo-frame.png")) as UIView
cell.AlbumImage?.image = UIImage(named: Albums[indexPath.row])
if self.navigationItem.rightBarButtonItem!.title == "Edit" {
cell.CloseImage?.hidden = true
} else {
cell.CloseImage?.hidden = false
}
//Layer property in Objective C =>
"http://iostutorialstack.blogspot.in/2014/04/how-to-assign-custom-tag-or-value-to.html"
cell.CloseImage?.layer.setValue(indexPath.row, forKey: "index")
cell.CloseImage?.addTarget(self, action: "deletePhoto:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func deletePhoto(sender:UIButton) {
let i : Int = (sender.layer.valueForKey("index")) as Int
Albums.removeAtIndex(i)
self.collectionView!.reloadData()
}
}
// AlbumCell.swift
// UICollectionView+Swift
import UIKit
class AlbumCell: UICollectionViewCell {
@IBOutlet var AlbumImage : UIImageView?
@IBOutlet var CloseImage : UIButton?
}
// ProfileController.swift
// UICollectionView+Swift
import UIKit
import QuartzCore
class ProfileController: UIViewController {
@IBOutlet var BannerImage : UIImageView!
@IBOutlet var ProfileImage : UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
//1. Circule profile picture
//var layer : CALayer = self.ProfileImage?.layer
self.ProfileImage.layer.cornerRadius = self.ProfileImage.frame.size.width / 2
self.ProfileImage.layer.borderWidth = 3.5
self.ProfileImage.layer.borderColor = UIColor.whiteColor().CGColor
self.ProfileImage.clipsToBounds = true
//2. Rectangle Profile shape
/*
var layer:CALayer = self.ProfileImage.layer!
layer.shadowPath = UIBezierPath(rect: layer.bounds).CGPath
layer.shouldRasterize = true;
layer.rasterizationScale = UIScreen.mainScreen().scale
layer.borderColor = UIColor.whiteColor().CGColor
layer.borderWidth = 2.0
layer.shadowColor = UIColor.grayColor().CGColor
layer.shadowOpacity = 0.4
layer.shadowOffset = CGSizeMake(1, 3)
layer.shadowRadius = 1.5
self.ProfileImage.clipsToBounds = false
*/
// Do any additional setup after loading the view.
}
}
// UTILITY.swift
// UICollectionView+Swift
=> Here Used One Of The Random Method Class Whereto take Images randomly:-
import UIKit
class UTILITY: NSObject {
class func getRandomNumberBetween (From: Int , To: Int) -> Int {
return From + Int(arc4random_uniform(UInt32(To - From + 1)))
}
}
// AlbumViewController.swift
// UICollectionView+Swift
import UIKit
let reuseIdentifier = "Cell"
class AlbumViewController: UICollectionViewController {
var Albums = Array<String>()
@IBAction func EditAlbumPressed(sender : AnyObject) {
if(self.navigationItem.rightBarButtonItem?.title == "Edit"){
self.navigationItem.rightBarButtonItem?.title = "Done"
for item in self.collectionView!.visibleCells() as [AlbumCell] {
var indexpath : NSIndexPath = self.collectionView!.indexPathForCell(item as AlbumCell)!
var cell : AlbumCell = self.collectionView!.cellForItemAtIndexPath(indexpath) as AlbumCell
//Profile Picture
//var img : UIImageView = cell.viewWithTag(100) as UIImageView
//img.image = UIImage(named: "q.png") as UIImage
//Close Button
var close : UIButton = cell.viewWithTag(102) as UIButton
close.hidden = false
}
} else {
self.navigationItem.rightBarButtonItem?.title = "Edit"
self.collectionView?.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
Albums = ["a.png", "b.png", "c.png", "d.png", "e.png", "f.png", "g.png", "h.png",
"i.png", "j.png", "k.png", "l.png", "m.png"]
}
// #pragma mark UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView?) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView?,
numberOfItemsInSection section: Int) -> Int {
return Albums.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:
NSIndexPath) -> UICollectionViewCell {
/*
We can use multiple way to create a UICollectionViewCell.
*/
//1.
//We can use Reusablecell identifier with custom UICollectionViewCell
/*
let cell = collectionView!.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
as UICollectionViewCell
var AlbumImage : UIImageView = cell.viewWithTag(100) as UIImageView
AlbumImage.image = UIImage(named: Albums[indexPath.row])
*/
//2.
//here Created a Class file for UICollectionViewCell
let cell : AlbumCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",
forIndexPath: indexPath) as AlbumCell
cell.backgroundView = UIImageView(image: UIImage(named: "photo-frame.png")) as UIView
cell.AlbumImage?.image = UIImage(named: Albums[indexPath.row])
if self.navigationItem.rightBarButtonItem!.title == "Edit" {
cell.CloseImage?.hidden = true
} else {
cell.CloseImage?.hidden = false
}
//Layer property in Objective C =>
"http://iostutorialstack.blogspot.in/2014/04/how-to-assign-custom-tag-or-value-to.html"
cell.CloseImage?.layer.setValue(indexPath.row, forKey: "index")
cell.CloseImage?.addTarget(self, action: "deletePhoto:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func deletePhoto(sender:UIButton) {
let i : Int = (sender.layer.valueForKey("index")) as Int
Albums.removeAtIndex(i)
self.collectionView!.reloadData()
}
}
// AlbumCell.swift
// UICollectionView+Swift
import UIKit
class AlbumCell: UICollectionViewCell {
@IBOutlet var AlbumImage : UIImageView?
@IBOutlet var CloseImage : UIButton?
}
// ProfileController.swift
// UICollectionView+Swift
import UIKit
import QuartzCore
class ProfileController: UIViewController {
@IBOutlet var BannerImage : UIImageView!
@IBOutlet var ProfileImage : UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
//1. Circule profile picture
//var layer : CALayer = self.ProfileImage?.layer
self.ProfileImage.layer.cornerRadius = self.ProfileImage.frame.size.width / 2
self.ProfileImage.layer.borderWidth = 3.5
self.ProfileImage.layer.borderColor = UIColor.whiteColor().CGColor
self.ProfileImage.clipsToBounds = true
//2. Rectangle Profile shape
/*
var layer:CALayer = self.ProfileImage.layer!
layer.shadowPath = UIBezierPath(rect: layer.bounds).CGPath
layer.shouldRasterize = true;
layer.rasterizationScale = UIScreen.mainScreen().scale
layer.borderColor = UIColor.whiteColor().CGColor
layer.borderWidth = 2.0
layer.shadowColor = UIColor.grayColor().CGColor
layer.shadowOpacity = 0.4
layer.shadowOffset = CGSizeMake(1, 3)
layer.shadowRadius = 1.5
self.ProfileImage.clipsToBounds = false
*/
// Do any additional setup after loading the view.
}
}
// UTILITY.swift
// UICollectionView+Swift
=> Here Used One Of The Random Method Class Whereto take Images randomly:-
import UIKit
class UTILITY: NSObject {
class func getRandomNumberBetween (From: Int , To: Int) -> Int {
return From + Int(arc4random_uniform(UInt32(To - From + 1)))
}
}