Swift - 带有自定义单元格的项目未显示在我的表格视图中 [英] Swift - Items not showing up in my table view with customized cell

查看:26
本文介绍了Swift - 带有自定义单元格的项目未显示在我的表格视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

因此,在完成所有这些操作之后,我将团队数组除以 2 来返回应该有多少个表格单元格.然后我将单元格中的数据添加到每个标签中.问题是当视图加载时,它只显示一个普通的表视图,不显示任何数据.

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.

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

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...

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

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

推荐答案

先设置 tableViewdelegatedatasource 之后,而不是返回 count/2 个数组,返回对象较少的数组的个数.

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

现在在 numberOfRowsInSection 中返回数组之一的计数.

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
}

这篇关于Swift - 带有自定义单元格的项目未显示在我的表格视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆