【问题标题】:I really don't know how to make collectionViewCell (didSelected) function work properly我真的不知道如何让collection View Cell (isSelected) 功能正常工作
【发布时间】:2020-05-06 07:28:33
【问题描述】:

我正在使用 Swift 5 制作日历,但现在遇到了问题。 首先这是我的代码:

import UIKit

class YearViewController: UIViewController {

    @IBOutlet weak var yearName: UILabel!
    @IBOutlet weak var yearCollectionView: UICollectionView!

    override func viewDidLoad() {

        super.viewDidLoad()
        yearName.text = String(CalenderBrain.init().curruntYear)
        yearCollectionView.dataSource = self
        yearCollectionView.delegate = self

        yearCollectionView.register(UINib(nibName: "ReusableYearCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "MyCell3")

        if let layout = self.yearCollectionView.collectionViewLayout as? UICollectionViewFlowLayout{
            layout.minimumInteritemSpacing = -10
            layout.minimumLineSpacing = 1
        }

    }

}

extension YearViewController : UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell3", for: indexPath) as! ReusableYearCollectionViewCell
        cell.monthName.text = Calendar.current.monthSymbols[indexPath.row]
        if cell.monthName.text == CalenderBrain.init().curruntMonthName(){
            cell.monthName.textColor = .red
            cell.tag = 999
        }
        cell.month = indexPath.row + 1
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return .init(width: 126.3, height:(570-3)/4)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = storyboard?.instantiateViewController(withIdentifier: "lastOne") as? ViewController
        vc?.whatIGetFromYearViewController = indexPath.row + 1
        vc?.curruntMonthNameThatIHaveToPut = Calendar.current.monthSymbols[indexPath.row]
        vc?.nextMonthNameThatIHaveToPut = Calendar.current.monthSymbols[indexPath.row + 1]
        self.navigationController?.pushViewController(vc!, animated: true)
        print("i'm pressed")
    }
}

我希望如果我的单元格被点击然后发生转场。

这是我的牢房的图像。 (我在 collectionViewCell 中制作 collectionView。)

问题是仅当我单击标签的左侧部分时才会出现转场。如果我在 CollectionViewCell 中单击 collectionView,则不会发生 segue。我认为那是因为 Swift 在 collectionViewCell 中识别出另一个 CollectionView 中的 collectionView。所以现在,我想让collectionViewCell点击并发生segue,无论我点击什么部分。这可能吗?

【问题讨论】:

  • 尝试在collectionViewCell中设置collectionView.isUserInteractionEnable = false
  • 哇真的非常感谢!!我让它成为现实
  • 好的,我为你的问题发布我的答案

标签: ios swift uicollectionview segue


【解决方案1】:

尝试在 collectionViewCell 中设置collectionView.isUserInteractionEnable = false

此代码将阻止用户对 collectionViewCell 中的 collectionView 进行操作,只接收主 collectionView 的 didSelectItem 操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 2015-02-01
    • 2021-07-04
    • 2016-01-08
    • 2013-10-31
    • 1970-01-01
    相关资源
    最近更新 更多