Swift - 项目未显示在我的 table 自定义单元格视图中

Swift - Items not showing up in my table view with customized cell

我的阵列团队没有出现在我的表格视图中。我正在寻找与此相关的常见问题。

在下面的代码中,我从我的 firebase 收集数据并将这些数据放入一个名为 teams 的数组中。在我得到团队数组之后,我将该数组放入两个数组中,因为我的自定义单元格中有两个标签,分别称为 teamOneLabel 和 teamTwoLable。

所以在我完成所有这些之后,我 return 应该有多少 table 个单元格,方法是将团队数组除以 2。然后我将单元格中的数据添加到每个标签中。问题是当视图加载时它只显示一个常规的 table 视图并且它不显示任何数据。

我认为数据可能需要一点点才能放入团队数组中。例如,如果我在 getTeamsAndTimes() 函数之后打印团队数组,它不会在控制台中显示任何内容,但是如果我在它向函数本身添加数据之后打印它,它就可以正常工作。

经过进一步研究,我意识到我的自定义单元格根本没有显示,这让我相信我的标识符或其他东西有问题。我不确定。

这是显示 table 视图的视图代码...

import UIKit
import Foundation
import Firebase


class CSGOView: UIViewController, UITableViewDelegate, UITableViewDataSource{
    @IBOutlet weak var tableViewView: UITableView!

    var teams: [String] = []
    var times: [String] = []
    var teamOneArray: [String] = []
    var teamTwoArray: [String] = []




    let teamsRef = FIRDatabase.database().reference(fromURL: "")
    let timesRef = FIRDatabase.database().reference(fromURL: "Not showing these")

    override func viewWillAppear(_ animated: Bool) {
        getTeamsAndTimes()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        getTeamsAndTimes()
        self.tableViewView.reloadData()
    }

    func getTeamsAndTimes() {
        //let userID = FIRAuth.auth()?.currentUser?.uid
        teamsRef.observeSingleEvent(of: .value, with: { (snapshot) in
            // Get user value
            for child in snapshot.children {
                if let team = (child as! FIRDataSnapshot).value as? String {
                    self.teams.append(team)
                }
            }
            self.findTeamOneAndTeamTwo(teams: self.teams)
            print(self.teamOneArray)
            print(self.teamTwoArray)
            //Reload the tabelView after that
            self.tableViewView.reloadData()
        }) { (error) in
            print(error.localizedDescription)
        }
        timesRef.observeSingleEvent(of: .value, with: { (snapshot) in
            // Get user value
            for child in snapshot.children {
                if let team = (child as! FIRDataSnapshot).value as? String {
                    self.times.append(team)
                }
            }

            //Reload the tabelView after that
            self.tableViewView.reloadData()
        }) { (error) in
            print(error.localizedDescription)
        }
    }

    func teamsWithTimes() -> [String : String] {
        var timesTeams: [String: String] = [:]



        return timesTeams
    }

    func findTeamOneAndTeamTwo(teams: [String]) {
        //Find the count then divide
        var i = 0

        //check if its even
        for team in teams{
            if i%2 == 0 {
                self.teamOneArray.append(team)
            } else {
                self.teamTwoArray.append(team)
            }

            i += 1
        }



    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.teams.count/2
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
        cell.TeamOneLabel?.text = teamOneArray[indexPath.row]
        cell.TeamTwoLabel?.text = teamTwoArray[indexPath.row]
        return cell

    }

}

这是自定义单元格的代码...

import UIKit

class CustomCell: UITableViewCell {
    @IBOutlet weak var TeamTwoLabel: UILabel!
    @IBOutlet weak var TeamTwoPhoto: UIImageView!
    @IBOutlet weak var TeamOnePhoto: UIImageView!
    @IBOutlet weak var TeamOneLabel: UILabel!

    override func awakeFromNib() {


        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

这是 table 视图的样子...

真正奇怪的是我在自定义单元格中有一个名为 VS 的标签,但它甚至没有显示。

这是包含单元格和 table视图

的故事板

首先设置 tableViewdelegatedatasource 之后而不是 returning array of count / 2 return count of array对象较少。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return teamOneArray < teamTwoArray ? teamOneArray.count : teamTwoArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
    cell.TeamOneLabel?.text = teamOneArray[indexPath.row]
    cell.TeamTwoLabel?.text = teamTwoArray[indexPath.row]
    return cell
}

如果你想显示数组中最后一个比你多一个值的对象,你需要向数组中少一个对象的数组添加空字符串。

func findTeamOneAndTeamTwo(teams: [String]) {
    //Find the count then divide
    var i = 0

    //check if its even
    for team in teams{
        if i%2 == 0 {
            self.teamOneArray.append(team)
        } else {
            self.teamTwoArray.append(team)
        }
        i += 1
    }
    if (self.teamOneArray.count != self.teamTwoArray.count) {
        if (self.teamOneArray.count < self.teamTwoArray.count) {
            self.teamOneArray.append("")
        }
        else {
            self.teamTwoArray.append("")
        }
    }
}

现在在 numberOfRowsInSection return 计数数组之一。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return teamOneArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
    cell.TeamOneLabel?.text = teamOneArray[indexPath.row]
    cell.TeamTwoLabel?.text = teamTwoArray[indexPath.row]
    return cell
}