【问题标题】:SWIFT: Prototype Cells not being loaded after performSegueWithIdentifierSWIFT:在 performSegueWithIdentifier 之后未加载原型单元
【发布时间】:2015-11-06 19:55:05
【问题描述】:

我有一个 ViewController 使用这个函数调用(点击一个按钮)另一个视图

@IBAction func btnSeeContact(sender: AnyObject) {
        self.performSegueWithIdentifier("segueSeeContact", sender: self)
    }

我的原型单元被“链接”到我创建并实现的名为 ContactsTableViewCell 的自定义视图控制器:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ContactsTableViewCell
        
        cell.txtName.text = "test"
        cell.txtPhone.text = "1234567890"
        
        return cell

    }

当我运行项目时,按钮调用了表格,但上面没有 Cell,我在那些 tableView 函数上设置了一个断点,它们没有被访问。

我在这里错过了什么,这些函数永远不会被调用?

【问题讨论】:

  • 您能否展示您的故事板的屏幕截图以便更好地理解?
  • 你写的是prototype cell is "linked",是Storyboard中的单元格标识符吗?
  • 也许您在包含表格视图的视图控制器的故事板中使用了错误的类名。这是您的 segue 所指向的。所以也许它根本不会加载您的自定义视图控制器。
  • 截图中的这个 TableViewController 是由导航控制器创建的。我创建的唯一其他视图控制器是 ContactsTableVillCell,它保留了单元格的标签。诸如“numberOfRowsInASection”之类的功能在主视图控制器中实现

标签: ios swift uitableview ios9 xcode7


【解决方案1】:

我正在添加一个新答案,因为我之前的答案被投票赞成,所以我不想对那个答案进行大量修改,并且仍然是解决您的问题的有效方法。

问题是您对自定义类感到困惑。在您的屏幕截图中,您可以看到 Table View Controller 未设置为自定义类,它只是显示Table View Controller。那就是需要获取 UITableViewController 类的自定义实现的对象。

相反,您似乎将单元的类设置为自定义类,并在那里实现委托方法。表格视图单元格仍然需要一个自定义类,但它应该是UITableViewCell 的自定义类。

所以你的单元格应该是这样的:

import UIKit
class YourCustomTableViewCell: UITableViewCell {
  @IBOutlet weak var yourLabel1: UILabel!
  @IBOutlet weak var yourLabel2: UILabel!
}

您将获得此单元格的一个实例以在 cellForIndexPath 中进行配置。 因此,您的表视图控制器类应设置为如下所示的类。 YourTableViewController 是你想要实现所有的委托方法。

注意:如果您使用的是从情节提要中拖出的 UITableViewController,则它已经拥有 tableView,并且已经为您连接好了委托/数据源的东西。您还将注意到您正在覆盖委托方法,因为 UITableViewController 类具有这些的默认实现。如果您只是使用普通视图控制器,请参阅我之前的回答,了解有关如何设置的更多详细信息。

import UIKit

class YourTableViewController: UITableViewController {

  override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
  }

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 1
  }

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)     
    if let cell = cell as? YourCustomTableViewCell {
      cell.yourLabel1.text = "some text"
      cell.yourLabel2.text = "some other text"
    }
    return cell
  }   
}

【讨论】:

  • 很好,现在可以了。我创建了一个新的 ContactTableViewController 并将其作为我的表格视图的自定义类。我也没有结束“numberOfSectionsInTableView”。
  • 我真的需要创建这个新的 TableViewController 吗?我不能只使用“主”ViewController 来实现 tableView 功能吗?
  • 谢谢!这对我帮助很大:)
【解决方案2】:

正如其他人所评论的,您确实需要提供更多背景信息。

这里有一些可能出错的地方,提供更多上下文可以证实或否认这种猜测。

首先你不显示 numberOfSectionsInTableView 方法。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
          return 0
  }

我认为您需要提供 0 以外的值

其次,由于在我确定您打算成为 UITableViewDelegate 方法函数调用的前面没有看到override,这意味着您的视图控制器不是 UITableViewController。这让我想知道您是否将此视图控制器定义为符合 UITableViewDelegate 协议,以及您是否将表视图出口委托设置为 self. (甚至将 UITableView 连接到插座)

如果您使用普通的 UIViewController 来托管表格视图,您需要执行以下操作:

  1. 将 UITableView 连接到视图控制器中的插座
  2. 声明视图控制器符合 UITableViewDeleagate(也可能是 UITableViewDataSource)协议
  3. 将表视图的出口委托(可能还有数据源)属性设置为 self(实现协议的视图控制器)
  4. 实现所需的方法

所以是这样的:

class MyTableViewController: UIViewController, UITableViewDelegate {

  @IBOutlet weak var tableView: UITableView!

  override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
  }

  func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
  }

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 1
  }

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("your PrototypeCell", forIndexPath: indexPath)
    // Configure the cell...
    return cell
  }
}

【讨论】:

  • 现在可以了。我创建了另一个控制器,一个 TableViewController,并将它作为我的表视图的自定义类。我也没有过度使用“numberOfSectionsInTableView”功能。我真的需要创建这个新控制器吗?我不能使用“主”视图控制器来覆盖 tableView 功能吗?
猜你喜欢
  • 1970-01-01
  • 2015-11-21
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多