Swift - 项目视图中没有显示的项目,具有自定义单元格

时间:2017-03-20 02:30:09

标签: swift uitableview tableviewcell

我的阵列小组没有出现在我的tabe视图中。我正在寻找这方面的常见问题。

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

所以在我完成所有这些之后,然后通过将teams数组除以2来返回应该有多少个表格单元格。然后我将单元格中的数据添加到每个标签中。问题是,当视图加载时,它只显示常规表视图,并且不显示任何数据。

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

经过进一步的研究后,我意识到自定义单元格根本没有显示,导致我认为我的标识符有什么问题。我不确定。

以下是显示表格视图的视图代码...

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
    }

}

这是表视图的样子......

The View

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

这是包含cell和tableview的故事板

1 个答案:

答案 0 :(得分:0)

首先设置delegate的{​​{1}}和datasource,而不是返回具有较少对象的数组tableView返回数组。

count / 2

如果要显示数组的最后一个对象,该对象的值超过了将空字符串添加到少一个对象的数组所需的值。

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