CollectionView Methods

//MARK: COLLECTIONVIEW CONFIG //Collection View (Declaration) let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.scrollDirection = UICollectionViewScrollDirection.Horizontal layout.minimumInteritemSpacing = 4 layout.minimumLineSpacing = 4 layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1) layout.itemSize = CGSize(width: 50, height: 50) myCollectionView.collectionViewLayout = layout myCollectionView.dataSource = self myCollectionView.delegate = self myCollectionView.allowsMultipleSelection = false myCollectionView.registerNib(UINib(nibName: "AttachmentCell", bundle: nil), forCellWithReuseIdentifier: "Cell") myCollectionView.backgroundColor = UIColor.clearColor() //MARK: COLLECTION VIEW extension MyViewController: UICollectionViewDelegate, UICollectionViewDataSource { func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 5 } func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { //Just Allow Header type guard kind == UICollectionElementKindSectionHeader else { return UICollectionReusableView() } let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCollectionReusableView", forIndexPath: indexPath) as! HeaderCollectionReusableView header.lblTitle.text = "My String" return header } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! UICollectionViewCell cell.textLabel.text = "My String" return cell } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("Tap in cell \(indexPath.row) on section \(indexPath.section)") } }
The basic way to declare and implement a Collection View

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.