I made a custom segmented control with reference to "http://bitly.kr/fDz2Ma". However, when I select a particular item, I need to see the collection view cell that matches the item, but I have not found the selected index of the segmented control.
I have created custom segmented controls to date but have not found the selected index button item.
import UIKit
class MainBookViewController: UIViewController {
@IBOutlet weak var interfaceSegmented: CustomSegmentedControl! {
didSet{
interfaceSegmented.setButtonTitles(buttonTitles: ["A","B","C"])
interfaceSegmented.selectorViewColor = .red
interfaceSegmented.selectorTextColor = .red
}
}
override func viewDidLoad() {
super.viewDidLoad()
codeSegmentedConfig()
}
func codeSegmentedConfig() {
let codeSegmented = CustomSegmentedControl(frame: CGRect(x: 0, y: 90, width: self.view.frame.width, height: 50), buttonTitle: ["A","B","C""])
codeSegmented.backgroundColor = .clear
view.addSubview(codeSegmented)
}
}
extension MainBookViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView
.dequeueReusableCell(withReuseIdentifier: "MainBookCell", for: indexPath) as! MainBookCell
cell.bookTitleLabel.text = "apple"
cell.bookWriterLabel.text = "me"
cell.bookImageView.image = UIImage(named: "3")
return cell
}
}
If I only know the button index of the selected custom segmented control, I want to use the case statement to change the data in the collection view cell to the index number.
If I only know the index of the selected button item, I plan to modify the collection view cell using the case statement in the CollectionViewDataSource code.
If I understood your question properly, you could achieve that by doing something like this:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch interfaceSegmented.selectedSegmentIndex {
case 0:
//do something when selected segment index is 0
case 1:
//do something when selected segment index is 1
default:
break
}