How to Set custom UICollectionView View border in swift
Follow this steps and get custom collectionview
STEP 1:- Create New Project "CustomCollectionViewDemo"
STEP 2:- Go main.Storybord and Drag and drop UICollectionView.
STEP 3:- set The consternt.
STEP 4:- Copy and Pest this code.
import UIKit
class CustomCollectionViewDemo : UIViewController, UICollectionViewDataSource , UICollectionViewDelegate
{
// MARK:- ---------- OUTLET Declaration ------------
@IBOutlet var myCollectionObj: UICollectionView!
// MARK:- ---------- Variable Declaration ------------
var myArray = [String]()
// MARK:- ---------- VIEW LifeCycel ------------
override func viewDidLoad() {
super.viewDidLoad()
myArray = ["Item 1","Item 2","Item 3","Item 4"]
}
func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return myArray.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell (withReuseIdentifier: "CategoryCollectionViewCell", for: indexPath) as! CategoryCollectionViewCell
cell.myView.frameframe.size.width = 75
cell.myView.frameframe.size.height = 20
cell.myView.layer.masksToBounds = true
cell.myView.layer.cornerRadius = 5
cell.myView.layer.borderWidth = 1
cell.myView.layer.borderColor = UIColor.blue
cell.myLabel.text = myArray[indexPath.row]
cell.cancelBtn.tag = indexPath.row
cell.cancelBtn.addTarget(self, action: #selector(cancelButton_action(_:)), for: .touchUpInside)
return cell
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
//code
}
//MARK:- ------- Objective Functions ---------
@Objc func cancelButton_action(_ sender:UIBUtton)
{
myArray.remove(at: sender.tag)
myCollectionObj.reloadData()
}
}
// MARK:- my CollectionView cell class
class ItemCell: UICollectionViewCell
{
// MARK:- ---------- OUTLET Declaration ------------
@IBOutlet var myView: UIView!
@IBOutlet var myLabel: UILabel!
@IBOutlet var cancelBtn: UIButton!
}
I Hop you Enjoy this tutorial keep touch in this blog for more update
STEP 1:- Create New Project "CustomCollectionViewDemo"
STEP 2:- Go main.Storybord and Drag and drop UICollectionView.
STEP 3:- set The consternt.
STEP 4:- Copy and Pest this code.
import UIKit
class CustomCollectionViewDemo : UIViewController, UICollectionViewDataSource , UICollectionViewDelegate
{
// MARK:- ---------- OUTLET Declaration ------------
@IBOutlet var myCollectionObj: UICollectionView!
// MARK:- ---------- Variable Declaration ------------
var myArray = [String]()
// MARK:- ---------- VIEW LifeCycel ------------
override func viewDidLoad() {
super.viewDidLoad()
myArray = ["Item 1","Item 2","Item 3","Item 4"]
}
func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return myArray.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell (withReuseIdentifier: "CategoryCollectionViewCell", for: indexPath) as! CategoryCollectionViewCell
cell.myView.frameframe.size.width = 75
cell.myView.frameframe.size.height = 20
cell.myView.layer.masksToBounds = true
cell.myView.layer.cornerRadius = 5
cell.myView.layer.borderWidth = 1
cell.myView.layer.borderColor = UIColor.blue
cell.myLabel.text = myArray[indexPath.row]
cell.cancelBtn.tag = indexPath.row
cell.cancelBtn.addTarget(self, action: #selector(cancelButton_action(_:)), for: .touchUpInside)
return cell
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
//code
}
//MARK:- ------- Objective Functions ---------
@Objc func cancelButton_action(_ sender:UIBUtton)
{
myArray.remove(at: sender.tag)
myCollectionObj.reloadData()
}
}
// MARK:- my CollectionView cell class
class ItemCell: UICollectionViewCell
{
// MARK:- ---------- OUTLET Declaration ------------
@IBOutlet var myView: UIView!
@IBOutlet var myLabel: UILabel!
@IBOutlet var cancelBtn: UIButton!
}
I Hop you Enjoy this tutorial keep touch in this blog for more update
Comments
Post a Comment