Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. import UIKit
  2.  
  3. //defining a enum to track cell state
  4.  
  5. enum CellState {
  6. case selected
  7. case unselected
  8.  
  9. init() {
  10. self = .unselected
  11. }
  12. }
  13.  
  14. class yourCell: UICollectionViewCell {
  15.  
  16. @IBOutlet weak var yourBtn: UIButton!
  17.  
  18. // creating our func
  19. var buttonAction: (() -> ())?
  20.  
  21. override func awakeFromNib() {
  22. super.awakeFromNib()
  23. }
  24.  
  25.  
  26.  
  27. @IBAction func sliderAction(_ sender: UISlider) {
  28.  
  29. // defining our func where we want to use it
  30. buttonAction()
  31. }
  32.  
  33. }
  34.  
  35.  
  36. // inside our viewController
  37. import UIKit
  38.  
  39. class yourViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
  40. @IBOutlet weak var collectionView: UICollectionView!
  41.  
  42. // you data source that you fill the cells
  43. var yourDataArray: [yourDataSource]()
  44.  
  45. // the array that will hold status of our cells
  46. private var cellStateArray: [CellState]?
  47.  
  48.  
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51.  
  52. //confirming our delegate and dataSource
  53. collectionView.dataSource = self
  54. collectionView.delegate = self
  55.  
  56. //filling your cellStateArray as many as your cell count
  57. self.cellStateArray = Array(repeating: .unselected, count: self.yourDataArray!.count )
  58. }
  59.  
  60.  
  61. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  62. if yourDataArray?.isEmpty == false {
  63. return yourDataArray!.count
  64. } else {
  65. print("Caution yourDataArray IS EMPTY")
  66. return 0
  67. }
  68. }
  69.  
  70. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  71.  
  72. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "yourCell", for: indexPath) as! yourCell
  73.  
  74. // checking your button selected or not while reuse it (while scrolling)
  75.  
  76. if cellStateArray![indexPath.row] == .selected {
  77. cell.yourBtn.backgroundColor = .red
  78. } else {
  79. cell.yourBtn.backgroundColor = .blue
  80. }
  81.  
  82.  
  83. cell.buttonAction = {
  84.  
  85. //calling button function
  86. button_Select(_ sender: cell.yourBtn, cellStateArray: cellStateArray, indexPath: IndexPath)
  87.  
  88. }
  89.  
  90. }
  91.  
  92.  
  93. // Defining the function what you want to do with your button in each cell
  94.  
  95. func button_Select(_ sender: UIButton, cellStateArray: [CellState], indexPath: IndexPath ) {
  96. sender.isSelected = !sender.isSelected
  97. if sender.isSelected{
  98. sender.backgroundColor = .red
  99.  
  100. //setting value when your button selected
  101. cellStateArray[indexPath.row] = .selected
  102. }
  103. else{
  104. sender.backgroundColor = .blue
  105.  
  106. //setting value when your button unselected
  107. cellStateArray[indexPath.row] = .unselected
  108. }
  109. collectionView.reloadData()
  110. }
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement