在 ios swift 中的相应按钮上单击时在同一视图控制器中显示和隐藏两个 xib
show and hide two xibs in same view controller on respective button click in ios swift
我有一个视图控制器,我在其中获取了一个视图,在该视图中我有两个按钮,在该视图下方我有一个集合视图。我为每个相应的按钮点击创建了 2 个自定义 xib。默认情况下,我为按钮 1 设置了 xib 1,但是当我单击按钮 2 时,我不知道如何显示 xib 2
按钮 1 的屏幕截图结果:
按钮 2 的屏幕截图结果:
xib 类别的屏幕截图:
[
xib 商店的屏幕截图:
我在视图控制器文件中的代码是:
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)
}
}
不要在 storeData
方法中隐藏 categoryColView
更改 属性 的 category_title
、store_title
按钮选择并重新加载集合视图。
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()
}
}
在集合视图数据源和委托方法中,检查 category_title
、store_title
按钮的 isSelected 状态并根据该状态执行操作。
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
}
}
}
发生这种情况是因为您在 collectionView 中仅 "CategoryCell1" 出队。
您可以做的一件事是创建一个枚举,例如:TypeCells
enum TypeCells {
case categorie
case store
}
然后你可以在你的控制器中初始化一个变量,例如:
var typeCell: TypeCells = .categorie
然后在你的IBAction中,你可以修改你的变量,例如:
@IBAction func storeData(_ sender: UIButton) {
typeCell = .data
collectionView.reloadData()
}
接下来您只需:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch typeCell {
case categorie:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoryCell1", for: indexPath) as! CategoryCell1
// what you want to display
case storie:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoresCell", for: indexPath) as! CategoryCell1
// what you want to display
}
玩得开心! :)
我有一个视图控制器,我在其中获取了一个视图,在该视图中我有两个按钮,在该视图下方我有一个集合视图。我为每个相应的按钮点击创建了 2 个自定义 xib。默认情况下,我为按钮 1 设置了 xib 1,但是当我单击按钮 2 时,我不知道如何显示 xib 2
按钮 1 的屏幕截图结果:
按钮 2 的屏幕截图结果:
xib 类别的屏幕截图:
[
xib 商店的屏幕截图:
我在视图控制器文件中的代码是:
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)
}
}
不要在 storeData
方法中隐藏 categoryColView
更改 属性 的 category_title
、store_title
按钮选择并重新加载集合视图。
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()
}
}
在集合视图数据源和委托方法中,检查 category_title
、store_title
按钮的 isSelected 状态并根据该状态执行操作。
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
}
}
}
发生这种情况是因为您在 collectionView 中仅 "CategoryCell1" 出队。
您可以做的一件事是创建一个枚举,例如:TypeCells
enum TypeCells {
case categorie
case store
}
然后你可以在你的控制器中初始化一个变量,例如:
var typeCell: TypeCells = .categorie
然后在你的IBAction中,你可以修改你的变量,例如:
@IBAction func storeData(_ sender: UIButton) {
typeCell = .data
collectionView.reloadData()
}
接下来您只需:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch typeCell {
case categorie:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoryCell1", for: indexPath) as! CategoryCell1
// what you want to display
case storie:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoresCell", for: indexPath) as! CategoryCell1
// what you want to display
}
玩得开心! :)