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

风格不统一 提交于 2019-12-08 05:13:55

问题


My array teams is not showing up in my tabe view. I am looking for common problems with this.

In the code below I gather data from my firebase and put that data into an array called teams. after I get the teams array I then put that array into two arrays because I have two labels in my custom cell called teamOneLabel and teamTwoLable.

So after I do all that I then return how many table cells there should be by dividing the teams array by 2. Then I add the data from the cells into each label. The problem is when ever the view loads it shows just a regular table view and it does not display any data.

I think it is possible that the data takes a little bit to be put into the teams array. For example if I print the teams array after the getTeamsAndTimes() function it will display nothing in the console, but If I print it after it adds the data in the function itself it works fine.

After further reasearch I have realized that I the custom cell is not displaying at all leading me to believe there is something wrong with my identifier or something. I'm not sure.

Here is the code for the view that shows the table view...

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

    }

}

here is the code for the custom 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
    }

}

here is what the table view looks like...

What's really wierd is that I have a label in the custom cell called VS and thats not even showing up.

that is the storyboard with the cell and tableview


回答1:


First set the delegate and datasource of tableView after that instead of returning array of count / 2 return count of array that have less object.

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
}

If you want to show the last object of array that have one more value than you need to add empty string to the array that have one less object.

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("")
        }
    }
}

Now in numberOfRowsInSection return count for one of the array.

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
}


来源:https://stackoverflow.com/questions/42894935/swift-items-not-showing-up-in-my-table-view-with-customized-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!