在ios swift中单击相应按钮在同一视图控制器中显示和隐藏两个xib

时间:2019-06-13 14:21:44

标签: swift xcode collectionview

我有一个视图控制器,在其中有一个视图,在那个视图中,我有两个按钮,在该视图下面,有一个集合视图。我分别为每个按钮单击创建2个自定义Xib。默认情况下,按钮1我设置了xib 1,但是当我单击按钮2时,不知道如何显示xib 2

按钮1的屏幕截图结果: screenshot for categories button

按钮2的屏幕截图结果: screenshot for stores button

类别xib的屏幕截图: [screenshot for category xib

商店xib的屏幕截图: screenshot for store 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)
        }
}

2 个答案:

答案 0 :(得分:0)

不要在categoryColView方法中隐藏storeData

更改选择的category_titlestore_title按钮的isSelected属性,然后重新加载集合视图。

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_titlestore_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
        }
    }
}

答案 1 :(得分:0)

发生这种情况是因为您仅在collectionView中使“ CategoryCell1”出队。

您可以做的一件事就是创建一个枚举,例如:TypeCells

IntroduceInstabilityByIgnoringProtectedModeSettings()

然后您可以在Controller中初始化变量,例如:

enum TypeCells {
 case categorie
 case store
}

然后在IBAction中,您可以修改变量,例如:

var typeCell: TypeCells = .categorie

接下来,您只需要:

@IBAction func storeData(_ sender: UIButton) {
        typeCell = .data
        collectionView.reloadData()
    }

玩得开心! :)