【问题标题】:Table view cell not showing in xcode表格视图单元格未在 xcode 中显示
【发布时间】:2016-09-03 05:55:06
【问题描述】:

我有这个视图控制器(见下面的代码)链接到一个表视图控制器故事板。我得到了实际的表格视图,但没有显示我的自定义单元格。关于我做错了什么的任何想法?

很高兴知道: - 我有一个专门为我的手机制作的单独的 swift 文件 - 我已将单元与单元标识符连接起来 - 这不是我的主视图控制器。

提前致谢!

import UIKit


class ForecastTableViewController: UITableViewController {

var forecasts = [Forecast]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.reloadData()

}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return forecasts.count 
}

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

    if let cell = tableView.dequeueReusableCell(withIdentifier: "forecastCell", for: indexPath) as? ForecastCell {

        let forecast = forecasts[indexPath.row]
        cell.configureCell(forecast: forecast)
        return cell

    } else {

        return ForecastCell()

    }

}

}

【问题讨论】:

  • 给定 delegatedatasourceUITableView?
  • 你设置了tableview的delegate和datasource吗?
  • 我已经做了,还是不行
  • 尝试numberOfRowsInSection下的静态计数
  • 你确定预测不是一个空数组吗?

标签: ios swift xcode


【解决方案1】:

viewDidload上面的重载函数中添加以下行,

  self.tableView.delegate = self
self.tableView.dataSource = self

viewDidload 中不需要self.tableView.reloadData(),所以删除它!

【讨论】:

  • 我现在已经添加了这些行。但我收到错误“'UITableView' 类型的值没有成员'数据源'。”并且将 UITableViewDataSource 添加到类中不起作用,因为它已经是 UITableViewController。
  • 它是 dataSource 而不是 datasource。这是一个错字!
【解决方案2】:

把它放在你的 viewDidLoad() 方法中

self.tableView.registerNib(UINib(nibName: "ForecastCell", bundle: nil), forCellReuseIdentifier: "forecastCell")

还要确保您实际设置了单元格的标识符而不是恢复 ID(常见错误)

【讨论】:

  • 感谢您的帮助。虽然仍然无法正常工作......正如我所说,我得到了表格视图,但没有得到我的自定义单元格。我想也许可以重做整个单元格,看看是否可行。
【解决方案3】:

您是否注册了您的手机,如果没有先注册。

tableView.registerClass(MyCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)

或者,如果您使用带有 Xib 的单元格,则注册

self.tableView.registerNib(UINib(nibName: "UICustomTableViewCell", bundle: nil), forCellReuseIdentifier: "UICustomTableViewCell")

【讨论】:

  • 感谢您的帮助!我编写了以下代码,没有收到任何错误。 'tableView.register(ForecastCell.self, forCellReuseIdentifier: "forecastCell")' 但是单元格还是不行...
  • 检查分配给单元格的标识符,您必须选择以上两个中的任何一个。您是使用 xib 还是在 storybaord 中创建自定义?
  • 标识符是“forecastCell”,我没有使用Xib,所以我选择了你的第一个建议。
  • 如果一切正常,则从情节提要中删除一个表并重新连接所有内容。
  • 我只保留了下面的代码来测试它是否正常工作,检查一下。覆盖 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("forecastCell", forIndexPath: indexPath) return cell }
【解决方案4】:

尝试以这种方式更改您的代码,并记住将您的数据放入您的 forecasts 数组中

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "forecastCell", for: indexPath) as? ForecastCell 

        let forecast = forecasts[indexPath.row]
        cell.configureCell(forecast: forecast)
        return cell

    }

【讨论】:

    猜你喜欢
    • 2015-05-29
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    相关资源
    最近更新 更多