Ios UITableViewRows不可见

Ios UITableViewRows不可见,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个简单的UITableViewController,没有一行显示。表视图标题和节标题都在那里。我从未遇到过这种情况。想法 import UIKit class DashboardTableViewController: UITableViewController { var countyName: String? var sections = ["Quota Over/Short", "Qualified Renewals"

我有一个简单的UITableViewController,没有一行显示。表视图标题和节标题都在那里。我从未遇到过这种情况。想法

import UIKit

class DashboardTableViewController: UITableViewController {
    
    var countyName: String?
    var sections = ["Quota Over/Short", "Qualified Renewals", "Membership Dues"]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if countyName != nil {
            self.title = "\(countyName!) County"
        } else {
            self.title = "County Info"
        }
        
        let headerView = DashboardCollectionView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 200))
        tableView.tableHeaderView = headerView
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        } else if section == 1 {
            return 2
        } else {
            return 9
        }
    }
    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section]
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")

        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "UITableViewCell")
        }
        
        if #available(iOS 13.0, *) {
            cell?.textLabel?.textColor = .label
        } else {
            cell?.textLabel?.textColor = .black
        }
        cell?.textLabel?.font = UIFont(name: "HelveticaNeue-Regular", size: 25)
        cell?.textLabel?.numberOfLines = 2
        
        if indexPath.section == 0 {
            cell?.textLabel?.text = "-500"
        } else if indexPath.section == 1 {
            if indexPath.row == 0 {
                cell?.textLabel?.text = "Previous Year: 1500"
            } else {
                cell?.textLabel?.text = "Current Year: 1000"
            }
        } else {
            cell?.textLabel?.text = "Test Dues"
        }
        
        return cell!
    }
}

从图像上看,UITableView似乎为您的单元格分配了空间。我建议通过以下方式测试电池:

  • 不要重复使用手机只是为了看看你能不能找到什么东西
    let cell=UITableViewCell(样式:。默认,reuseIdentifier:nil)
  • 将背景色设置为某个颜色
    cell.backgroundColor=.red

  • 如果您看到单元格显示,那么您知道至少tableview为您提供了正确的单元格#

    您没有回答关于原型或基于代码的单元格类的问题,并且您显示的代码(在cellForRowAt中)不是使用表视图单元格的正确方式

    在不查看自定义单元格设置的情况下,使用“默认”单元格进行如下尝试:

    结果:


    如果这样做有效(将有效),请尝试使用此代码找出自定义单元格不工作的原因。

    添加infoLabel的背景并选中Debug hierarchy view:是否看到单元格?显示单元格类。。。它是故事板原型吗?还是纯粹的代码?奇怪的是,您会使用
    cell.infoLabel?.text
    。。。如果单元格的
    infoLabel
    是可选的,那么如果标签创建不正确,代码将不会执行任何操作。@DonMag查看我的更新。我尝试使用默认的tableview单元格,但遇到了相同的问题。我投票关闭此问题,因为标题中使用的集合视图中的OP代码是导致此问题的原因,因此此问题的答案无法解决此问题。我尝试了这两个建议,但都没有成功。我还将每行的标签文本打印到控制台,并获得正确的值。我不再使用自定义单元格。我使用的是默认单元格。我的故事板没有任何原型。@Ragingoat-好的。。。此代码不使用原型单元。它对你有用吗?不,它也在做同样的事情。那么你一定有其他的代码在改变什么。使用新的
    UITableViewController
    ,并使用我发布的确切代码将其自定义类分配给
    DashboardTableViewController
    。它会给你这个结果。看起来是我的DashboardCollectionView表格视图标题导致了这个问题。我用一个类似于您的简单视图替换了它,我的原始代码运行良好。
    class DashboardTableViewController: UITableViewController {
        
        var countyName: String?
        var sections = ["Quota Over/Short", "Qualified Renewals", "Membership Dues"]
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            if countyName != nil {
                self.title = "\(countyName!) County"
            } else {
                self.title = "County Info"
            }
            
            // I don't have your DashboardCollectionView class, so I'm creating
            //  as plain UIView with a blue background
            //let headerView = DashboardCollectionView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 200))
            let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 200))
            headerView.backgroundColor = .blue
            
            tableView.tableHeaderView = headerView
            
            // register a default UITableViewCell
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: "defaultCell")
        }
        
        // MARK: - Table view data source
        
        override func numberOfSections(in tableView: UITableView) -> Int {
            return sections.count
        }
        
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if section == 0 {
                return 1
            } else if section == 1 {
                return 2
            } else {
                return 9
            }
        }
        
        override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return sections[section]
        }
        
        //override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        //  return UITableView.automaticDimension
        //}
        
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            // don't do it like this
            //var cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")
            //if cell == nil {
            //  cell = UITableViewCell(style: .default, reuseIdentifier: "UITableViewCell")
            //}
            
            // dequeue a registered cell
            let cell = tableView.dequeueReusableCell(withIdentifier: "defaultCell", for: indexPath)
            
            if #available(iOS 13.0, *) {
                cell.textLabel?.textColor = .label
            } else {
                cell.textLabel?.textColor = .black
            }
            cell.textLabel?.font = UIFont(name: "HelveticaNeue-Regular", size: 25)
            cell.textLabel?.numberOfLines = 2
            
            if indexPath.section == 0 {
                cell.textLabel?.text = "-500"
            } else if indexPath.section == 1 {
                if indexPath.row == 0 {
                    cell.textLabel?.text = "Previous Year: 1500"
                } else {
                    cell.textLabel?.text = "Current Year: 1000"
                }
            } else {
                cell.textLabel?.text = "Test Dues"
            }
            
            return cell
        }
    
    }