how to implement single selection swapping of the cell in collection view swift - uicollectionview

I am having a collection view when i select a cell it should change the colour of the cell to yellow and when i select the next cell it should change its colour yellow but it should change the previous cell colour back to clear and i only want at a time 1 selected cell all the other remaining cells should remain clear color and only the selected should be in the yellow colour
Can anyone help me with this
var selectedFirstIndexPath = [IndexPath]()
var selectedSecondIndexPath : IndexPath?
var selection = false
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = detailedCollectionView.dequeueReusableCell(withReuseIdentifier: "cellZain", for: indexPath) as? BillingDetailVCollectionCell else {
return UICollectionViewCell()
}
cell.titleLabel.text = colltnTitleName?[indexPath.row].name
if self.selectedFirstIndexPath != [] && [indexPath] == self.selectedFirstIndexPath {
cell.viewBG.backgroundColor = UIColor.yellow
cell.titleLabel.textColor = UIColor.black
selection = true
}else {
cell.viewBG.backgroundColor = UIColor.clear
cell.titleLabel.textColor = UIColor.white
selection = false
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = detailedCollectionView.cellForItem(at: indexPath) as? BillingDetailVCollectionCell
self.selectedSecondIndexPath = indexPath
detailedCollectionView.allowsMultipleSelection = false
cell?.viewBG.backgroundColor = UIColor.yellow
cell?.titleLabel.textColor = UIColor.black
self.selection = true
if self.selectedFirstIndexPath != [] {
detailedCollectionView.delegate?.collectionView?(detailedCollectionView, didDeselectItemAt: indexPath)
self.selectedFirstIndexPath = []
self.selection = false
cell?.viewBG.backgroundColor = UIColor.clear
cell?.titleLabel.textColor = UIColor.white
DispatchQueue.main.async {
self.detailedCollectionView.reloadItems(at:self.selectedFirstIndexPath)
}
}
}

Related

CABasicAnimation stops when UICollectionView is reloaded

I need help with an issue in Collection Views. In my collectionView I am using UILongPressGestureRecognizer to start CABasicAnimation in the items (images), but when I remove an item and reload the collectionView the CABasicAnimation is interrupeted.
How can I prevent the animation stop?
I need the animation continues until the user decide to stop.
Here is some information about my collectionView:
class SentMemesCollectionVC: UICollectionViewController {
//MARK: - PROPERTIES
var memes: [Meme]! {
return Meme.accessMemes().memes
}
//MARK: - LIFE CYCLE
override func viewDidLoad() {
super.viewDidLoad()
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
self.collectionView.addGestureRecognizer(longPressGesture)
}
#objc func longTap(_ gesture: UILongPressGestureRecognizer) {
switch gesture.state {
case .began:
print("LongPress begin")
guard let selectedIndexPath = collectionView.indexPathForItem(at: gesture.location(in: collectionView)) else {return}
collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
setEditing(true, animated: true)
default:
collectionView.cancelInteractiveMovement()
}
}
// MARK: - COLLECTIONVIEW DATA SOURCE
//Defini o número de itens em cada seção
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return memes.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! GridMemeCell
let meme = memes[indexPath.row]
cell.prepareGridCell(with: meme)
cell.delegate = self
return cell
}
// MARK: - COLLECTIONVIEW DELEGATE
/*
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let memeDetail = storyboard?.instantiateViewController(withIdentifier: "DetailVC") as! MemeDetailVC
let meme = memes[indexPath.row]
memeDetail.memeSelected = meme
Feedback.share.hapticFeedback()
navigationController?.pushViewController(memeDetail, animated: true)
}
*/
// MARK: - DELETE ITEMS IN COLLECTVIEW
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
Feedback.share.hapticFeedback()
if let indexPaths = collectionView?.indexPathsForVisibleItems {
for indexPath in indexPaths {
if let cell = collectionView?.cellForItem(at: indexPath) as? GridMemeCell {
if editing {
cell.isEditing = editing
cell.startAnimate()
doneBarButtonItem()
} else {
cell.isEditing = editing
cell.stopAnimate()
addBarButtonItem()
}
}
}
}
}
}
Here is my CollectionViewCell
class GridMemeCell: UICollectionViewCell {
//MARK: - OUTLETS
#IBOutlet weak var ivImage: UIImageView!
#IBOutlet weak var deleteMeme: UIVisualEffectView!
//MARK: - PROPERTIES
weak var delegate: GridMemeCellDelegate?
var isAnimate: Bool! = true
//MARK: - METHODS AND COMPUTED PROPERTIES
func prepareGridCell(with meme: Meme) {
ivImage.image = meme.memeImage
deleteMeme.layer.cornerRadius = deleteMeme.bounds.width / 2.0
deleteMeme.layer.masksToBounds = true
deleteMeme.isHidden = !isEditing
deleteMeme.contentView.backgroundColor = Theme.current.subViewColor
}
func startAnimate() {
let shakeAnimation = CABasicAnimation(keyPath: "transform.rotation")
shakeAnimation.duration = 0.05
shakeAnimation.repeatCount = 4
shakeAnimation.autoreverses = true
shakeAnimation.duration = 0.2
shakeAnimation.repeatCount = 99999
let startAngle: Float = (-2) * 3.14159 / 180
let stopAngle = -startAngle
shakeAnimation.fromValue = NSNumber(value: startAngle)
shakeAnimation.toValue = NSNumber(value: 3 * stopAngle)
shakeAnimation.autoreverses = true
shakeAnimation.timeOffset = 290 * drand48()
let layer: CALayer = self.layer
layer.add(shakeAnimation, forKey: "animate")
self.deleteMeme.isHidden = false
isAnimate = true
}
func stopAnimate() {
let layer: CALayer = self.layer
layer.removeAnimation(forKey: "animate")
self.deleteMeme.isHidden = true
isAnimate = false
}
var isEditing: Bool = false {
didSet {
deleteMeme.isHidden = !isEditing
}
}
#IBAction func btDeleteMeme(_ sender: Any) {
Feedback.share.hapticFeedback()
delegate?.deleteCell(cell: self)
}
}
And here is my Protocol Delegate Cell:
protocol GridMemeCellDelegate: class {
func deleteCell(cell: GridMemeCell)
}
And here is my extension CollectionView - Delegate.
extension SentMemesCollectionVC: GridMemeCellDelegate {
func deleteCell(cell: GridMemeCell) {
if let indexPath = collectionView.indexPath(for: cell) {
//Apaga o Meme do Array
Meme.accessMemes().memes.remove(at: indexPath.item)
collectionView.reloadData()
}
}
}

Collection view cell not appear after resize the CollectionView height

I am facing the UICollectionView cell size issue. I am using two collection_view and one table_view 1:- Main CollectionView ,which is use for swipe and only display one cell at one time 2:- Inside Main Collection_View, i use table_view and now further inside table_view I am using Collection view which display 6 cell at one time.
Now the whole process is I am using a layout which is swipable and display 6 item at one page and so on.
The problem is when I set main cell height 200 then inside collectionview cell not appearing as expected.
Outer CollectionView Class
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var collectionMain: UICollectionView!
#IBOutlet weak var tableView: UITableView!
let itemsPerRow: CGFloat = 1
let sectionInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController:UICollectionViewDataSource,UICollectionViewDelegate {
//1
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
//2
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return 3
}
//3
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:cellCollectionCollectionViewCell! = collectionView.dequeueReusableCell(withReuseIdentifier: "cellSwipe",
for: indexPath) as! cellCollectionCollectionViewCell
switch indexPath.item {
case 0:
cell.backgroundColor = UIColor.black
cell.setupCollectionTableCell(indexPath)
break
case 1: cell.backgroundColor = UIColor.red
cell.setupCollectionTableCell(indexPath)
break
default:
cell.backgroundColor = UIColor.yellow
cell.setupCollectionTableCell(indexPath)
break
}
// Configure the cell
return cell
}
}
extension ViewController : UICollectionViewDelegateFlowLayout {
//1
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
//2
let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
let availableWidth = self.collectionMain.frame.width - paddingSpace
let widthPerItem = availableWidth / itemsPerRow
return CGSize(width: widthPerItem, height: widthPerItem)
}
//3
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return sectionInsets
}
// 4
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return sectionInsets.left
}
}
Model Constant class
import UIKit
class Constant {
static let totalItem: CGFloat = 9
static let column: CGFloat = 3
static let minLineSpacing: CGFloat = 1.0
static let minItemSpacing: CGFloat = 1.0
static let offset: CGFloat = 1.0 // TODO: for each side, define its offset
static func getItemWidth(boundWidth: CGFloat) -> CGFloat {
let totalWidth = boundWidth - (offset + offset) - ((column - 1) * minItemSpacing)
return totalWidth / column
}
}
Table Cell Class
import UIKit
class CollectionCell: UITableViewCell {
#IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.sectionInset = UIEdgeInsetsMake(
Constant.offset, // top
Constant.offset, // left
Constant.offset, // bottom
Constant.offset // right
)
layout.minimumInteritemSpacing = Constant.minItemSpacing
layout.minimumLineSpacing = Constant.minLineSpacing
}
collectionView.isScrollEnabled = false
collectionView.dataSource = self
collectionView.delegate = self
}
}
extension CollectionCell: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return Int(Constant.totalItem)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BoxCell", for: indexPath)
return cell
}
}
extension CollectionCell: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemWidth = Constant.getItemWidth(boundWidth: collectionView.bounds.size.width)
return CGSize(width: itemWidth, height: 50)
}
}
Main CollectionCell class
import UIKit
class cellCollectionCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var tblView: UITableView!
var indexPath:IndexPath?
func setupCollectionTableCell(_ row:IndexPath){
self.indexPath = row
self.tblView.delegate = self
self.tblView.dataSource = self
// self.tblView.estimatedRowHeight = 300
self.tblView.reloadData()
}
}
extension cellCollectionCollectionViewCell:UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.row {
case 0:
return 50
case 1:
let itemHeight = Constant.getItemWidth(boundWidth: tableView.bounds.size.width)
let totalRow = ceil(Constant.totalItem / Constant.column)
let totalTopBottomOffset = Constant.offset + Constant.offset
let totalSpacing = CGFloat(totalRow - 1) * Constant.minLineSpacing
let totalHeight = ((itemHeight * CGFloat(totalRow)) + totalTopBottomOffset + totalSpacing)
print("total height \(totalHeight)")
return totalHeight
default:
return UITableViewAutomaticDimension
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// switch indexPath.row {
// case 0:
// let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath)
// return cell
// default:
// let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
// return cell
// }
let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
return cell
}
When I set height of Main Collection view 200
Bellow are the link of images
When I set height 300, working fine

UICollectionView cell needs to be tapped twice for segue to happen

I know this will be something simple; however, I have created a UICollectionView that displays just some colours for now. When a cell is tapped I want it to perform a segue with the cell number that was tapped.
At the moment, if I tap a cell nothing happens. If I touch the same cell again nothing happens, but if I touch any other cell the segue occurs successfully and the cell clicked gets passed on, albeit with the first cells touched number.
It is in a navigation stack. Here is my code:
//Everything Collection View
//header text
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionHeader", for: indexPath as IndexPath) as! SectionHeaderPractice
header.headerLabel.text = "SELECT A TOPIC..."
return header
}
//number of cells
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
//height and width of cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellWidth = (view.frame.width/2)-20
let cellHeight = view.frame.height/4
return CGSize(width: cellWidth, height: cellHeight)
}
//cell content
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let content = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as UICollectionViewCell
//set text
let desc = content.viewWithTag(3) as! UILabel
desc.text = shieldTextArray[(indexPath as NSIndexPath).row]
//set color scale
let score = scoreArray[(indexPath as NSIndexPath).row]
let redC = 2*score
let greenC = 2*(1-score)
content.backgroundColor = UIColor.init(red: CGFloat(redC), green: CGFloat(greenC), blue: 0.45, alpha: 1)
return content
}
override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
//sets variable to clicked cell
//cellClicked = indexPath.row
//changes background colour on clicking
//let content1 = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as UICollectionViewCell
//content1.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 1)
//Performs segue
//print(cellClicked)
performSegue(withIdentifier: "practiceSegue", sender: self)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(10,10,10,10)
}
//End Everything Collection View
Any pointers appreciated.
Your issue is with this line didDeselectItemAt, make sure you use didSelectItemAt.
didDeselectItemAt is the reason why you have to tap twice for segue, you select the cell then deselect the cell, once the cell is deselected, your code perform segue.

Dynamic TableView Swift 3 With Beginning Graph

I have a tableview that gets its information from a database and returns it as "results. The TableView Data Source is as follows:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isLoading {
print("isLoading")
return 1
}else if results.count == 0 {
return 1
} else {
print("results Count:", results.count)
return results.count
}
}
enum MyRows: Int {
case graph = 0
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == MyRows.graph.rawValue {
let graphcell = tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifiers.GraphCell,for: indexPath) as! PendingGraphCell
graphcell.configureForGraph(graphData: self.graphData)
return graphcell
}
print(indexPath)
if isLoading {
let cell = tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifiers.loadingCell, for:indexPath)
return cell
} else if results.count == 0 {
return tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifiers.nothingFoundCell,for: indexPath)
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifiers.ResultCell,for: indexPath) as! ResultCell
let result = results[indexPath.row];
print(result)
cell.configureForResult(result: result)
return cell
}
}
The tableView works perfectly when I remove the "Graph" Cell:
if indexPath.row == MyRows.graph.rawValue {
let graphcell = tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifiers.GraphCell,for: indexPath) as! PendingGraphCell
graphcell.configureForGraph(graphData: self.graphData)
return graphcell
}
So I need to update the number of rows to include the 1st static row. Did that and then I receive an error.
Index out of range.
Table Views really confuse me, I've been working on this all night. Any help would be appreciated.
EDIT:
I edited the count to 4 and still got the same error. And results.count = 3
Here is the error:
Index out of range
Here is the Exception Breakpoint where it fails
let pendingResult = pendingResults[indexPath.row];
Also, my output cell for the graph is different than the one for the results cell. The results cell is 44 and so every other cell in my table is set to 44.. What gives?
Denis Angell
try this code.
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == MyRows.graph.rawValue {
let graphcell = tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifiers.GraphCell,for: indexPath) as! PendingGraphCell
graphcell.configureForGraph(graphData: self.graphData)
return graphcell
}
else{
var cell = tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifiers.ResultCell,for: indexPath) as! ResultCell
if isLoading {
cell = tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifiers.loadingCell, for:indexPath)
} else if results.count == 0 {
cell = tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifiers.nothingFoundCell,for: indexPath)
} else {
cell = tableView.dequeueReusableCell(withIdentifier: TableViewCellIdentifiers.ResultCell,for: indexPath) as! ResultCell
let result = pendingResults[indexPath.row];
print(result)
cell.configureForResult(result: result)
}
return cell
}
}

CollectionView does not work when embedded in Navigation Controller

I have a simple collection view test (based on an online tutorial) which works fine stand alone. But when I embed it in a navigation controller it stops working. I built the screen in code by (1) creating a headerView (64 pixel high) and added it to the view at the top. (2) I built a collection view and added it to the headerView.
Here is the code:
import UIKit
class ViewController: UIViewController,
UICollectionViewDelegate, UICollectionViewDataSource,
UINavigationControllerDelegate
{
var collectionView : UICollectionView!
var topView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
var frame = CGRect(x:0,y:128, width:view.frame.width, height:64)
topView = UIView(frame:frame)
self.view.addSubview(topView)
// CollectionView
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
layout.itemSize = CGSize(width: 50, height: 50)
frame = CGRect(x: 0, y: 0, width: Int(self.topView.frame.width), height: Int(self.topView.frame.height))
collectionView = UICollectionView (frame: frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
collectionView.backgroundColor = UIColor.green
self.topView.addSubview(collectionView)
}
//MARK: - CollectionView
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 14
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath as IndexPath)
for v in cell.subviews {
v.removeFromSuperview()
}
cell.backgroundColor = UIColor.orange
let label = UILabel(frame: CGRect(x:0 , y:0 , width:50 , height:50))
label.text = "\(indexPath.item)"
label.textAlignment = .center
label.textColor = UIColor.white
cell.addSubview(label)
return cell
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I couldn't make Kaushal's suggestion work as stated. It did give me a clue that my positioning of the view in which I had embedded the collection view was being misplaced in viewDidLoad for reasons that I have not understood. However by putting the collection view configuration in viewDidAppear (rather than viewDidLoad) it worked well. I offset the y position by 64 to clear the navbar, and I reduced the row height to 64. I also, put code to execute the code only once so that navigating from pages does not add multiple views of top of each other. BTW, my original objective was to have horizontally scrolling cells. In my program I have tableview with corresponding sections and the idea is to use the row with horizontally scrolling cells to move to a corresponding section.
The code is shown below:
//
// CustomViewController.swift
// DSM Tracker
//
// Created by Syed Tariq on 1/7/17.
// Copyright © 2017 com.syedtariq. All rights reserved.
//
import UIKit
class ViewController: UIViewController,
UICollectionViewDelegate,
UICollectionViewDataSource,
UINavigationControllerDelegate
{
var executeOnce = true
var cellDimensions = [String:Int]()
var cellHeight = 50
var cellWidth = 120
var collectionContainerView: UICollectionView!
var navBar: UINavigationBar = UINavigationBar()
// view constants
var viewY = CGFloat()
var viewX = CGFloat()
var viewWidth = CGFloat()
var viewHeight = CGFloat()
// gaps from view edge
let leftGap = CGFloat(20)
let rightGap = CGFloat(20)
// navbar constants
let navBarHeight = CGFloat(64)
var headerLabels = ["Cell 01","Cell 02","Cell 03","Cell 04","Cell 05","Cell 06","Cell 07","Cell 08","Cell 09","Cell 10","Cell 11","Cell 12","Cell 13","Cell 14","Cell 15","Cell 16"]
override func viewDidLoad() {
super.viewDidLoad()
navBar.backgroundColor = UIColor.green
executeOnce = true
viewY = view.frame.origin.y
viewX = view.frame.origin.x
viewWidth = view.frame.width
viewHeight = view.frame.height
}
func configureCollectionView () {
if executeOnce {
executeOnce = false
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
layout.itemSize = CGSize(width: cellWidth, height: cellHeight)
let colWidth = viewWidth - leftGap - rightGap
let colX = viewX + leftGap
let colY = viewY + navBarHeight
let colHeight = CGFloat(64)
let frame = CGRect(x:colX, y:colY, width: colWidth, height: colHeight)
//let frame = CGRect.zero
collectionContainerView = UICollectionView (frame: frame, collectionViewLayout: layout)
collectionContainerView.dataSource = self
collectionContainerView.delegate = self
collectionContainerView.autoresizingMask = [.flexibleLeftMargin,.flexibleLeftMargin,.flexibleBottomMargin,.flexibleRightMargin, .flexibleHeight, .flexibleWidth]
collectionContainerView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
collectionContainerView.backgroundColor = UIColor.blue
collectionContainerView.allowsSelection = true
collectionContainerView.isScrollEnabled = true
collectionContainerView.setNeedsDisplay()
print("collectionContainerView.frame \(collectionContainerView.frame)")
view.addSubview(collectionContainerView)
}
}
override func viewDidAppear(_ animated: Bool) {
configureCollectionView()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("headerLabels.count \(headerLabels.count)")
return headerLabels.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath as IndexPath)
for v in cell.subviews {
v.removeFromSuperview()
}
let cellTitle = headerLabels[indexPath.row]
let cellTitleLines = cellTitle.components(separatedBy: " ")
let nLabels = cellTitleLines.count
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 8
let labelHeight = cellHeight / cellTitleLines.count
for i in (0 ..< nLabels) {
let frame = CGRect(x: 0, y: labelHeight * i, width: cellWidth, height: labelHeight)
let label1 = UILabel(frame: frame)
cell.backgroundColor = UIColor.lightGray
label1.numberOfLines = 1
label1.text = headerLabels[indexPath.row]
label1.textAlignment = .center
label1.textColor = UIColor.black
label1.clipsToBounds = true
label1.adjustsFontSizeToFitWidth = true
label1.text = cellTitleLines[i]
cell.addSubview(label1)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
collectionContainerView.scrollToItem(at:IndexPath(item: indexPath.item, section: 0), at: .centeredHorizontally, animated: false)
return true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionContainerView.cellForItem(at: indexPath)
print("cell = \(cell)")
collectionContainerView.scrollToItem(at:IndexPath(item: indexPath.item, section: 0), at: .centeredHorizontally, animated: false)
}
func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
return true
}
func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}
}