I've been trying to create a custom collectionView but the method func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
is not working for some reason.
I added the UICollectionViewDelegate
into the Controller, but is not working.
here some code:
//
// PacotesViagensViewController.swift
// Alura Viagens
//
// Created by Guilherme Golfetto on 04/01/21.
//
import UIKit
class PacotesViagensViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UICollectionViewDelegate, UISearchBarDelegate {
@IBOutlet weak var pacotesViagem: UICollectionView!
@IBOutlet weak var pesquisarViagens: UISearchBar!
@IBOutlet weak var labelContadorPacotes: UILabel!
let listaComTodasViagens:Array<Viagem> = ViagemDAO().retornaTodasAsViagens();
var listaViagens:Array<Viagem> = []
override func viewDidLoad() {
super.viewDidLoad()
listaViagens = listaComTodasViagens;
pacotesViagem.dataSource = self
pacotesViagem.delegate = self;
pesquisarViagens.delegate = self;
self.labelContadorPacotes.text = self.atualizaContagemLabel();
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.listaViagens.count;
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let celulaPacote = collectionView.dequeueReusableCell(withReuseIdentifier: "celulaPacote", for: indexPath) as! PacoteViagemCollectionViewCell;
let viagem = listaViagens[indexPath.row];
celulaPacote.labelTitulo.text = viagem.titulo;
return celulaPacote;
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//dividido por dois porque s?o duas colunas
let larguraCelula = collectionView.bounds.width / 2;
return CGSize(width: larguraCelula - 15, height: 160)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("HIIII") //STILL NOT WORKING
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
listaViagens = listaComTodasViagens;
if searchText != "" {
// o [c] é para virar case insetivea
//o [d] é para ignorar a acentua??o
let filtroListaViagem = NSPredicate(format: "titulo contains[c] %@", searchText);
let listaFiltrada:Array<Viagem> = (listaViagens as NSArray).filtered(using: filtroListaViagem) as! Array;
listaViagens = listaFiltrada;
}
self.labelContadorPacotes.text = self.atualizaContagemLabel();
pacotesViagem.reloadData();
}
func atualizaContagemLabel() -> String {
return listaViagens.count == 1 ? "1 pacote encontrado" : "(listaViagens.count) pacotes encontrado";
}
}
I'm pretty new in Swift development and I don't know what I'm missing here.
Thanks in advance
EDIT 1:
Add all the Controller Code
question from:
https://stackoverflow.com/questions/65907929/swift-uicollectionview-not-calling-method-didselectitemat 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…