影响UICollectionViewCell外观的UIButton

时间:2019-01-17 20:18:51

标签: ios swift uicollectionview

好的,我有一个ViewController,其中包含一个UICollectionView和一个UIButton。该按钮在集合视图中不是。当用户点击UICollectionView中的某个项目时,我有一些代码可以更改单元格中UIView的背景颜色以显示它已被选中。我为UIButton使用了类似的代码,当用户点击它时它会更改其背景颜色,因此它的作用就像是样式复选框。

我看到的问题是,当我在“收藏夹”视图中点击一个项目时,它会突出显示。但是,如果我点击该按钮,该按钮将正常突出显示,但是收藏夹视图将更改突出显示的项目。即使被点击的按钮不在集合视图中,也没有与集合视图的联系。

请参见以下图片: 第一张图片:我在“收藏夹”视图中点击了一个项目,看到该项目被正常突出显示。

第二张图片:请注意,在这里,当我点击“不确定”按钮时,收藏夹视图将更改,并且高亮显示到另一个单元格。但是DidSelectCellForItemAt中没有代码正在运行

import Foundation

class SchedulingVisitForViewController: SchedulingViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var continueButton: RoundedButton!
    @IBOutlet weak var notSureButton: RoundedButton!
    @IBOutlet weak var notSureButtonTop: NSLayoutConstraint!

    var collectionData = [[String: String]]()


    override func viewDidLoad() {
        super.viewDidLoad()
        self.appointment = fetchAppointment()
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionData.append([
            "image": "regularExam",
            "text": "Regular Exam",
            "data": "regular_exam"])
        self.collectionData.append([
            "image": "vaccines",
            "text": "Essential Vaccines",
            "data": "vaccines"])
        self.collectionData.append([
            "image": "meds",
            "text": "Parasite Meds",
            "data": "preventitive_meds"])
        self.collectionData.append([
            "image": "dog",
            "text": "My pet is sick",
            "data": "sick_pet"])

        self.notSureButton.layer.shadowColor = Constants.appColor.gray.light.cgColor
        self.notSureButton.layer.shadowOffset = CGSize(width: 0, height: 2.0)
        self.notSureButton.layer.shadowRadius = 3.0
        self.notSureButton.layer.shadowOpacity = 0.8
        self.notSureButton.layer.masksToBounds = false
    }

    override func viewDidLayoutSubviews(){
        self.collectionView.frame = CGRect(x: self.collectionView.frame.origin.x, y:  self.collectionView.frame.origin.y, width: self.collectionView.frame.size.width, height: CGFloat(2 * 170))
        self.notSureButtonTop.constant = CGFloat(2 * 170) + 20.0
        self.view.layoutIfNeeded()
        self.collectionView.reloadData()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.collectionData.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SelectVisitForCollectionViewCell", for: indexPath) as! SelectVisitForCollectionViewCell
        let item = indexPath.item

        cell.displayContent(image: UIImage(named: self.collectionData[item]["image"]!)!, text: self.collectionData[item]["text"]!)

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let cell = collectionView.cellForItem(at: indexPath) as? SelectVisitForCollectionViewCell {
            var categories = self.appointment?.categories
            if let index = categories!.index(of: self.collectionData[indexPath.item]["data"]!) {
                categories?.remove(at: index)
                cell.showSelected(false)
            } else {
                categories?.append(self.collectionData[indexPath.item]["data"]!)
                cell.showSelected(true)
            }

            self.appointment?.categories = categories!

            if (self.appointment?.categories.count)! > 0 {
                self.continueButton.isHidden = false
            } else {
                self.continueButton.isHidden = true
            }

            saveAppointment(data: self.appointment!)
        }
    }


    @IBAction func onNotSureButtonPressed(_ sender: Any) {
        var categories = self.appointment?.categories
        if let index = categories!.index(of: "not_sure") {
            categories?.remove(at: index)
            self.notSureButton.backgroundColor = UIColor.white
            self.notSureButton.setTitleColor(Constants.appColor.gray.dark, for: .normal)
        } else {
            categories?.append("not_sure")
            self.notSureButton.backgroundColor = Constants.appColor.yellow.main
            self.notSureButton.setTitleColor(UIColor.white, for: .normal)
        }

        self.appointment?.categories = categories!

        if (self.appointment?.categories.count)! > 0 {
            self.continueButton.isHidden = false
        } else {
            self.continueButton.isHidden = true
        }
        print(self.appointment?.categories)
        saveAppointment(data: self.appointment!)
    }

    @IBAction func onContinueButtonPressed(_ sender: Any) {
        self.parentPageboy?.scrollToPage(.next, animated: true)
    }
}

1 个答案:

答案 0 :(得分:-1)

caninster_exister指出的问题是我正在重新加载集合视图