Я действительно не знаю, как заставить функцию collectionViewCell (didSelected) работать правильно - PullRequest
0 голосов
/ 06 мая 2020

Я делаю календарь в 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")
    }
}

Я хочу, чтобы при щелчке по моим ячейкам происходил переход.

А это изображение моей ячейки. (Я делаю collectionView в collectionViewCell.)

enter image description here

Проблема в том, что переход возникает только тогда, когда я нажимаю левую часть метки. Если я нажимаю collectionView в CollectionViewCell, перехода не происходит. Я думаю, это потому, что Swift распознает collectionView в collectionViewCell другой CollectionView. Итак, на данный момент я хочу, чтобы щелкнул collectionViewCell, и переход произошел независимо от части, которую я щелкаю. Возможно ли это?

1 Ответ

0 голосов
/ 07 мая 2020

Попробуйте установить collectionView.isUserInteractionEnable = false в collectionViewCell.

Этот код предотвратит действие пользователя над collectionView в collectionViewCell, просто получите действие didSelectItem основного collectionView.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...