从服务器获取JSON数据后,无法单击UICollectionViewCell

时间:2018-04-30 09:26:41

标签: ios xcode uicollectionview swift4 uicollectionviewcell

我对Swift 4很新,从Android Studio进入Xcode。

我遇到了集合视图的问题,问题是如果我用可预先确定的数据填充可解码的结构,它可以单击它并传递给didSelectItemat函数。但是,当我从服务器获取JSON时,它不会。

我的问题是,是否有任何事情在我脑海中浮现,或者我是否遗漏了什么。

这是我正在使用的代码:

struct SelectJogos:Decodable {         让成功:布尔         让jogos:[Jogos]     }

struct Jogos: Decodable{
    let NumJogo: Int
    let EquipaA: String
    let EquipaB: String
    let Fase: String
    let Serie: String
    let CodComp: String
    let Data: String
    let Hora: String
    let Local: String
}

class SubmeteController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!
    var jogo = [Jogos]()

    override func viewDidLoad() {
        super.viewDidLoad()

         collectionView.dataSource = self

        //User em sessão
        let user = UserDefaults.standard.string(forKey: "username")
        print(user!)

        //MandaPost para o server
        let url = URL(string: "http://yourexemple.com/something")
        var request = URLRequest(url: url!)
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.httpMethod = "POST"
        let postString = "clube=\(user!)&opera=select"
        request.httpBody = postString.data(using: .windowsCP1252)

        //Receve a info do sv
        let task = URLSession.shared.dataTask(with: request){data, request, error in
            guard let data = data, error == nil else{
                print("error=\(error!)")
                return
            }
            do{
                let dataServer = try JSONDecoder().decode(SelectJogos.self, from: data)
                if dataServer.success != false{
                    self.jogo = dataServer.jogos
                    print(dataServer.success)
                }else{
                    print("nogames")
                }

                DispatchQueue.main.async {
                    print("Number of Data:\(self.jogo.count)")
                    self.collectionView.reloadData()
                }
            }catch let jsonERR{
                print("Error DO: ", jsonERR)
            }   
        }
            task.resume()
}
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("clicked")
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return jogo.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! SubmeteListaViewCell
        cell.NumJogo.text = String(jogo[indexPath.row].NumJogo)
        print(jogo[indexPath.row].CodComp)
        print(indexPath)
        return cell
    } 
}

1 个答案:

答案 0 :(得分:2)

您必须在viewDidLoad中设置委托 `

collectionView.delegate = self