Выбранный UICollectionViewCell и выполнение перехода - PullRequest
0 голосов
/ 30 апреля 2018

Желание сохранить выбранный пользователем заголовок ячейки коллекции в качестве локальной переменной, чтобы затем его можно было перейти к следующему представлению с помощью перехода. У меня проблемы с функцией didSelectItemAt, даже если я поместил туда инструкцию print, при выделении ячейки ничего не произойдет.

GoalsViewController

import Foundation
import UIKit

class GoalsViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!

    var selectedGoal: String = ""

    var goalArray = ["Goal 1", "Goal 2"]
    var imageArray = ["1", "2"]

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.dataSource = self
        collectionView.delegate = self
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToGoalDetail" {
            let secondVC = segue.destination as! GoalDetailViewController
            secondVC.goalSelectedOnHome = selectedGoal
        }
    }



}

extension GoalsViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

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

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedGoal = goalArray[indexPath.row]
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GoalCell", for: indexPath) as! GoalsCollectionViewCell
        cell.goalTitleLabel.text = goalArray[indexPath.row]
        cell.backgroundColor = UIColor.darkGray
        cell.featuredImageView.image = UIImage(named: "\(imageArray[indexPath.row])")

        return cell
    }


}

GoalsCollectionViewCell

import UIKit

class GoalsCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var featuredImageView: UIImageView!
    @IBOutlet weak var goalTitleLabel: UILabel!
    @IBOutlet weak var backgroundColorView: UIView!
}

GoalsDetailViewController

import UIKit

class GoalDetailViewController: UIViewController {
    @IBOutlet weak var closeButtonImage: UIImageView!

    @IBOutlet weak var goalDetailTitle: UILabel!
    @IBOutlet weak var closeButton: UIButton!

    var goalSelectedOnHome = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        goalDetailTitle.text = goalSelectedOnHome

        closeButton.setImage(UIImage(named: "closeIcon"), for: .normal)
        closeButton.addTarget(self, action: #selector(closeButtonTapped), for: .touchUpInside)

    }

    @objc func closeButtonTapped(sender:UIButton!) {
        self.dismiss(animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



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