SWIFT:在 performSegueWithIdentifier 之后未加载原型单元

SWIFT: Prototype Cells not being loaded after performSegueWithIdentifier

我有一个 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

    }

当我运行项目时,按钮调用了table,但是上面没有Cell,我在那些table视图函数上打了一个断点,它们是未联系到。

我在这里错过了什么,那些函数从未被调用过?

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

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

首先你没有显示 numberOfSectionsInTableView 方法。

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

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

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

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

  1. 将 UITableView 连接到视图控制器中的插座
  2. 声明视图控制器符合 UITableViewDeleagate(可能还有 UITableViewDataSource)协议
  3. 将 table 视图的出口委托(可能还有数据源)属性设置为自身(实现协议的视图控制器)
  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
  }
}

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

问题是您的自定义 classes 混淆了。在您的屏幕截图中,您可以看到 Table 视图控制器未设置为自定义 class,它只是显示 Table View Controller。那是需要获得 UITableViewController class.

的自定义实现的对象

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

的自定义 class

因此您的单元格 class 应如下所示:

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

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

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

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
  }   
}