前提・実現したいこと
Twitterのホーム画面のようなSNSアプリを作成中です。
しかしひとつのセルをタップしても用意している画面に遷移しません。
**遷移する方法は調べたつもりですが、そもそもセルをタップしても用意しているprint文
も反応がありません。**まだほとんど初学者です。どなたか教えてください
発生している問題・エラーメッセージ
CollectionViewCellのセル一つをタップしても反応がありません
以下のシュミレーター内の緑の枠内をタップすると遷移するようにしたいです。
エラーはありません
該当のソースコード
以下が該当のViewControllerです
import UIKit class ViewController: UIViewController { // @IBOutlet weak var registerButton: UIButton! @IBOutlet weak var AppealLIstCollectionView: UICollectionView! private let cellId = "cellId" override func viewDidLoad() { super.viewDidLoad() // AppealLIstCollectionView.isUserInteractionEnabled = false AppealLIstCollectionView.delegate = self AppealLIstCollectionView.dataSource = self AppealLIstCollectionView.register(UINib(nibName: "AppealListCell", bundle: nil), forCellWithReuseIdentifier: cellId) self.view.addSubview(AppealLIstCollectionView) // setupViews() } @IBAction func registerButton(_ sender: UIButton) { print("sign") let storyboard: UIStoryboard = UIStoryboard(name: "SignUp", bundle: nil) if let vc = storyboard.instantiateInitialViewController() { self.present(vc, animated: true, completion: nil) } } } extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print("didSelectItemAt") } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let width = self.view.frame.width return .init(width: width, height: width) } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 20 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = AppealLIstCollectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! AppealListCell return cell } }
試したこと
上記のようにタップされた時のコードを書いても、デバックにタップされた反応すらありません。
補足情報(FW/ツールのバージョンなど)
以下追記を頂けたら、回答が付きやすくなるかもしれません。
・CollectionViewのDelegateを設定しているソースの追記
・CollectionViewのCellの内容
※何をCell上に乗せているか(ImageViewなどは乗っているか)、コードはどうなっているか など
追記ありがとうございました。追加で確認ですが、以下も問題なさそうでしょうか。
・CollectionViewCell自体(AppealListCell.xib)の、「UserInteractionEnabled」にチェックが入っているか(trueになっているか)
・その他、Cell上のImageViewなども「UserInteractionEnabled」にチェックがはいっているか
コード自体を動作させたところタップが反応したので、違うとしたらCollectionView自体やCellの設定の違いかなと思いましたので、コメントさせて頂きました。
お返事ありがとうございます。確認しましたが、全てにチェックは入っておりました。
何度もお手数おかけしますが、他に補足の情報必要であればお伝えいただければと思います。
Delegateも設定されてますし、「UserInteractionEnabled」もチェックが入ってるとなると問題の切り分けが難しいですね。。
私からは以下をご提案させて頂きます。パッと解決できずで申し訳ないです。
・AppealListCel.swift のコードも質問文に追記してみる
・別プロジェクトで、シンプルなCollectionViewを配置してみる(それでタップ処理が動くなら、今回のプロジェクトと差分を比較してみる)
ありがとうございます。一応AppealListCel.swift のコードを追記しました。
こちらこそ親切丁寧に教えていただきありがとうございます。
あなたの回答
tips
プレビュー