i have a view controller in which i have taken a view and in that view i have two buttons and below that view i have a collection view. i have create 2 custom xibs for each respective button click. by default for button 1 i have set xib 1 but when i click button 2 then dont know how to show xib 2
screenshot result for button 1:
screenshot result for button 2:
screenshot for categories xib: [
my code in view controller file is:
class CategoryViewController: UIViewController {
@IBOutlet weak var store_bar: UIViewX!
@IBOutlet weak var store_title: UIButton!
@IBOutlet weak var category_title: UIButton!
@IBOutlet weak var category_bar: UIViewX!
@IBOutlet weak var categoryColView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// register collectionview cell
self.categoryColView.register(UINib(nibName: "CategoryCell1", bundle: nil), forCellWithReuseIdentifier: "CategoryCell1")
self.categoryColView.register(UINib(nibName: "StoresCell", bundle: nil), forCellWithReuseIdentifier: "StoresCell")
self.store_bar.isHidden = true
}
@objc func click_Category(sender: UIButton!) {
UIView.animate(withDuration: 1.0) {
sender.isSelected = !sender.isSelected
}
}
@IBAction func storeData(_ sender: UIButton) {
self.categoryColView.isHidden = true
self.store_bar.isHidden = false
self.store_title.setTitleColor(UIColor.black, for: .normal)
self.category_bar.isHidden = true
self.category_title.setTitleColor(UIColor(rgb: 0xAAAAAA), for: .normal)
}
@IBAction func categoriesData(_ sender: UIButton) {
self.categoryColView.isHidden = false
self.store_bar.isHidden = true
self.category_title.setTitleColor(UIColor.black, for: .normal)
self.category_bar.isHidden = false
self.store_title.setTitleColor(UIColor(rgb: 0xAAAAAA), for: .normal)
}
}
extension CategoryViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoryCell1", for: indexPath) as! CategoryCell1
cell.btn_click.tag = indexPath.row
cell.btn_click.setImage(#imageLiteral(resourceName: "image_unchecked"), for: .normal)
cell.btn_click.setImage(#imageLiteral(resourceName: "image_checked"), for: .selected)
cell.btn_click.addTarget(self, action: #selector(self.click_Category), for: .touchUpInside)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (UIScreen.main.bounds.width) / 3, height: 93)
}
}
Don't hide the categoryColView
in storeData
method
Change isSelected property of category_title
, store_title
buttons on selection and reload the collection view.
class CategoryViewController: UIViewController {
@IBOutlet weak var store_bar: UIViewX!
@IBOutlet weak var store_title: UIButton!
@IBOutlet weak var category_title: UIButton!
@IBOutlet weak var category_bar: UIViewX!
@IBOutlet weak var categoryColView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// register collectionview cell
self.categoryColView.register(UINib(nibName: "CategoryCell1", bundle: nil), forCellWithReuseIdentifier: "CategoryCell1")
self.categoryColView.register(UINib(nibName: "StoresCell", bundle: nil), forCellWithReuseIdentifier: "StoresCell")
storeData(store_title)
}
@objc func click_Category(sender: UIButton!) {
UIView.animate(withDuration: 1.0) {
sender.isSelected = !sender.isSelected
}
}
@IBAction func storeData(_ sender: UIButton) {
category_title.isSelected = false
store_title.isSelected = true
self.categoryColView.isHidden = true
self.store_bar.isHidden = false
self.store_title.setTitleColor(UIColor.black, for: .normal)
self.category_bar.isHidden = true
self.category_title.setTitleColor(UIColor(rgb: 0xAAAAAA), for: .normal)
self.categoryColView.reloadData()
}
@IBAction func categoriesData(_ sender: UIButton) {
category_title.isSelected = true
store_title.isSelected = false
self.categoryColView.isHidden = false
self.store_bar.isHidden = true
self.category_title.setTitleColor(UIColor.black, for: .normal)
self.category_bar.isHidden = false
self.store_title.setTitleColor(UIColor(rgb: 0xAAAAAA), for: .normal)
self.categoryColView.reloadData()
}
}
In collection view data source and delegate methods check isSelected status of the category_title
, store_title
buttons and perform actions based on that.
extension CategoryViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// if category_title.isSelected {
// return category count
// } else {
// return store count
// }
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if category_title.isSelected {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoryCell1", for: indexPath) as! CategoryCell1
cell.btn_click.tag = indexPath.row
cell.btn_click.setImage(#imageLiteral(resourceName: "image_unchecked"), for: .normal)
cell.btn_click.setImage(#imageLiteral(resourceName: "image_checked"), for: .selected)
cell.btn_click.addTarget(self, action: #selector(self.click_Category), for: .touchUpInside)
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoresCell", for: indexPath) as! StoresCell
//...
return cell
}
}
}