First, I'm sorry but i'm not good at English.
Anyway, i want to select all cells in UICollectionViewCell when i click UIButton outside UICollectionView. But i don't how can i do.
I think it will be possible using collections of UIButton like 5 stars. i just try it.
well.. click button below. The cells in blue is false.
if you click 'Everyday' Button, All cells will be red. The cells in red means it is chosen.
if you click 'Weekend' Button, the two rightmost cells will be red. now you know this UICollectionView means 'a week'. so, when 'Weekend' is selected, other 5 days will be blue.
i made this with UICollectionView. but i don't know how i control with button outside. if you need more information and code, or you don't understand i'll edit it or leave a comment. please tell me how.
ViewController code about UIButton and UICollectionView is here. and there is a imageview in CollectionViewCell... that's all...
import UIKit
class CreateViewController: UIViewController {
@IBOutlet weak var titleTextField: UITextField!
@IBOutlet weak var MotivTextField: UITextField!
@IBOutlet weak var repeatCollectionView: UICollectionView!
@IBAction func everydayBtn(_ sender: Any) {
// empty
}
@IBAction func weekdayBtn(_ sender: Any) {
// empty
}
@IBAction func weekendBtn(_ sender: Any) {
// empty
}
var selectedDay = [Bool]()
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0...7 {
self.selectedDay.append(false)
}
repeatCollectionView.delegate = self
repeatCollectionView.dataSource = self
repeatCollectionView.isScrollEnabled = false
}
}
extension CreateViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? RepeatDayCVCell {
cell.dayBtn.backgroundColor = UIColor.red
}
}
}
extension CreateViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RepeatDayCVCell", for: indexPath) as! RepeatDayCVCell
if self.selectedDay[indexPath.row] == true {
cell.dayBtn.backgroundColor = UIColor.red
}
else {
cell.dayBtn.backgroundColor = UIColor.blue
}
return cell
}
}
You will have to update your data model and reload the collection view on each button action.
@IBAction func weekendBtn(_ sender: Any) {
for index in 0..<selectedDay.count {
if index < 5 {
selectedDay[index] = false
} else {
selectedDay[index] = true
}
}
repeatCollectionView.reloadData()
}
@IBAction func weekdayBtn(_ sender: Any) {
for index in 0..<selectedDay.count {
if index < 5 {
selectedDay[index] = true
} else {
selectedDay[index] = false
}
}
repeatCollectionView.reloadData()
}
@IBAction func everydayBtn(_ sender: Any) {
for index in 0..<selectedDay.count {
selectedDay[index] = true
}
repeatCollectionView.reloadData()
}
If you want to update the collection view when tapping on cell, update the data in selectedDay
at that index and reload that row.
Pls excuse the formatting.