【问题标题】:UITableview: failed to obtain a cell from its dataSourceUITableview:无法从其数据源获取单元格
【发布时间】:2016-10-06 17:52:22
【问题描述】:

我收到此错误消息:failed to obtain a cell from its dataSource

奇怪的是

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

甚至没有被调用,问题不在于返回的单元格为零。我设置了断点。 cellForRowAt不会被调用。

numberOfRowsInSection 返回值。

我不知道它是否相关,但现在我不使用UITableViewController,我只有一个UIViewController,它是datasourcedelegateUITableView。我希望它不会造成干扰。有什么想法吗?

【问题讨论】:

  • 任何错误 sss?

标签: ios uitableview swift3


【解决方案1】:

交叉检查以下检查清单:-

  • 这是因为您未能使可重复使用的单元出列。
    • 问题是您的 cellForRowAtIndexPath 函数嵌入在另一个函数中
    • 当您忘记将 UITableViewDataSource 和 UITableViewDelegate 协议添加到 ViewController declaration 时。

类DataViewController:UIViewController、UITableViewDataSource、UITableViewDelegate

【讨论】:

  • 对,我忘了协议
  • 这种情况下does not conform to protocol 'UITableViewDataSource'怎么处理?
【解决方案2】:

我猜这是你的问题 - Failed to obtain a cell from its DataSource

你可以看看:https://www.hackingwithswift.com/example-code/uikit/fixing-failed-to-obtain-a-cell-from-its-datasource

试试这样的:

cell = tableView.dequeueReusableCellWithIdentifier(getCellIdentifierForType(cellType))
        setDataToCell(cell, item: item)

祝你好运

【讨论】:

    【解决方案3】:

    当您将代码从 swift 旧版本迁移到新版本时,请确保您的 tableview 数据源和委托已更新语法。 例如

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    

    无法自动更新到以下:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多