如何调试 UITableView 不显示行?

Posted

技术标签:

【中文标题】如何调试 UITableView 不显示行?【英文标题】:How can I debug UITableView not displaying rows? 【发布时间】:2018-10-23 14:02:40 【问题描述】:

我有一个视图控制器

class PlacesVC: UITableViewController 

    override func viewDidLoad() 
        super.viewDidLoad()
    

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int 
        return 1
    

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        return 4
    


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

        let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
        cell.textLabel?.text = "Row \(indexPath.row)"

        return cell
    

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
        performSegue(withIdentifier: "toMap", sender: kill)
    

我的单元格 ID 也为 Cell

当我构建和运行应用程序时,我不断得到空表

我希望得到这样的 4 行。

如何调试为什么我的行没有显示?

【问题讨论】:

UITableViewCell(style: .default, reuseIdentifier: "Cell") --> tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 我可以看到 4 与故事板中的默认 UITableViewController 和您共享的代码。你能分享更多细节吗? @PrashantTukadiya,不,我仍然看到空的。另外,我得到的当前有什么问题?它是过时的语法吗? @EmreÖnder。我能提供什么?我还不知道如何调试这个东西。我是 ios 新手。 实现 heightForRowAt ?? 【参考方案1】:

首先,确保您已将 tableView 的 delegatedataSource 属性设置为该 viewController 的实例(否则这些方法将不会被调用)。 (如果你的 ViewController 继承自 TableViewController,你可以跳过这一步)。

第二件事,您正在为每一行启动一个新的单元格实例,而不是重复使用它。 所以替换这一行:

    let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")

与:

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 

第三件事:确保你已经从 IB 设置了正确的类。

最后一件事:将样式从“自定义”更改为“基本”,以获得默认的textLabel

【讨论】:

其实他不需要设置那些属性因为已经是UITableViewController了。也许故事板有一个普通的 UIViewController 但类是 UITableViewController? 对不起,伙计们,原来我忘了给我的 tableVC 分配一个类。通过这样做:i.imgur.com/ALfXsVv.png,现在它可以工作了; 如果您想检查委托是否已连接,请向现有方法添加断点,如果它们被命中,则设置委托。我敢打赌他们不会被击中……【参考方案2】:

您需要在 Storyboard 上 ViewController 的 Identity Inspector 部分中设置您的课程。

【讨论】:

【参考方案3】:

你可以试试

func tableView(_ tableView: UITableView, 
     heightForRowAt indexPath: IndexPath) -> CGFloat 

   return 60

如果你想要动态

tableView.estimatedRowHeight = 60 
tableView.rowHeight = UITableViewAutomaticDimension

【讨论】:

问题是缺少类! 没有 heightForRowAt 函数,我仍然可以看到行。只是让您知道; 是的,但它是系统设置为在找不到满足您当前设置的 heightForRowAt 或动态单元格约束时避免崩溃的那些【参考方案4】:

检查您是否在代码中添加了 tableView delegatedataSource

注册一个类(如果您使用的是自定义类)以用于创建新的表格单元格。在viewDidLoad() 喜欢: tableView.register(your cell class, forCellReuseIdentifier:"identifier")

初始化单元格对象

tableView.dequeueReusableCell(WithIdentifier:,forIndexPath:)

万一您没有为您的单元使用任何课程,那么您不需要注册您的课程。 您只需通过以下方式初始化单元格对象:

tableView.dequeueReusable(CellWithIdentifier:)

【讨论】:

delegatedataSource 方法已经存在于代码中,并且它不需要 delegatedataSource 作为 PlacesVC 派生自 UITableViewController 在这种情况下不需要注册自定义类,因为 OP 已经将它们添加到情节提要中并将单元格的类类型设置为自定义类。它由情节提要自动注册。 在答案的最后一部分,如果有人隐式或显式注册任何单元格,tableView.dequeueReusable(CellWithIdentifier:) 将如何返回任何单元格? 不要混淆 UITableViewCellUICollectionViewCell。考虑为dequeueReusableCell 阅读UITableView 的appledoc。 对不起,它是 uitableviewcell 类

以上是关于如何调试 UITableView 不显示行?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 UITableView 中显示带有视频图像的视频文件?

从 XIB 加载的 UITableView Sectionheader 有时显示不正确

iOS:重新加载 UITableView 后滑动显示数据

如何减少/删除分组 UITableView 中的左/右手边距?

Swift 4 TableView Cell 不显示正确的图像

如何使用删除按钮在 UITableView 中显示重新排序控件