Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
840 views
in Technique[技术] by (71.8m points)

ios - Swift UICollectionView not calling method didSelectItemAt

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I checked your code. The delegate pattern is right. As I think, you have to implement datasource functions so that the collection view can have some items. According to the current code, there is not item in collection view. So didSelectItemAt event can't occur now. Please reference following code.

extension ViewController: UICollectionViewDataSource {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCell
        cell.textLabel.text = String(indexPath.row + 1)
        return cell
    }
}

extension ViewController: UICollectionViewDelegate {

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.item + 1)
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...