簡體   English   中英

在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的屏幕截圖

商店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_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
        }
    }
}

發生這種情況是因為您只在collectionView中將“CategoryCell1”出列。

你可以做的一件事是創建一個Enum,例如:TypeCells

enum TypeCells {
 case categorie
 case store
}

然后你可以在你的Controller中初始化一個變量,例如:

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

    }

玩得開心 ! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM