静态 Table 视图错误

Errors with Static Table Views

试图在我的应用程序中解决几个场景我无法让 tableViews 正常工作。

我正在执行类似 tableView 的设置,用户可以在 两种可能的设置 之间进行选择。每个选项都会导致 新 table视图,用户必须在其中选择一个可用选项。一旦用户选择了一个,第一个场景中的标签将显示所选的值(也存储为 NSUserDefaults)。因此,如果我没记错的话,这可以通过 3 个静态 tableView 来实现。下面的代码是我如何尝试这样做的:

初始设置视图控制器:

class SettingsVC2: UITableViewController{
 override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.section == 0{

        let cell = tableView.dequeueReusableCellWithIdentifier("office_cell", forIndexPath: indexPath)
        cell.textLabel?.text = "Select your office"
        return cell
}
    else {
        let cell = tableView.dequeueReusableCellWithIdentifier("dept_cell", forIndexPath: indexPath)
        cell.textLabel?.text = "Select your department"
        return cell
   }

其中一项设置table观看次数:

class SettingsDepartmentTVC: UITableViewController{

let departamentos: [String] = ["Sales", "Pre-Sales", "General Administration", "Customer Service", "Profesional Services", "Regionales"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return departamentos.count
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("department_cell", forIndexPath: indexPath)
    cell.textLabel?.text = self.departamentos[indexPath.row]
    return cell

}
}

我相信所有数据源和委托都已正确挂钩,即使未显示 UIKit 也已导入。

错误我得到:

Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier office_cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard


这是我尝试填充 tableView:

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


    let cell = tableView.dequeueReusableCellWithIdentifier("CommentsRowTVC", forIndexPath: indexPath) as! CommentsRowTVC

    let single_comment = self.AOS[indexPath.row]

    cell.titulo_comentario?.text = single_comment.titulo_comment
        cell.cuerpo_comentario?.text = single_comment.cuerpo_comment
    cell.fecha_comentario?.text = single_comment.fecha_comment
    return cell
}

class CommentsRowTVC: UITableViewCell {


@IBOutlet weak var titulo_comentario: UILabel!
@IBOutlet weak var cuerpo_comentario: UILabel!
@IBOutlet weak var fecha_comentario: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

这是 UIViewController 的连接检查器,其中 table 是:

这一个用于 UIViewCellController:

到目前为止的错误:

UIView tableView:numberOfRowsInSection: unrecognized selector sent to instance

Terminating app due to uncaught exception NSInvalidArgumentException, reason: [UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

如果您真的要制作 static table 视图单元格,那么您应该删除这些方法。仅当您动态地使单元格出队时才使用它们。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return departamentos.count
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("department_cell", forIndexPath: indexPath)
    cell.textLabel?.text = self.departamentos[indexPath.row]
    return cell

}

如果您实际上是在尝试制作 动态单元格 那么,正如您的错误消息所暗示的那样,您需要确保您的标识符与您在界面构建器中的标识符相匹配:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("{your_identifier}", forIndexPath: indexPath)
    cell.textLabel?.text = self.departamentos[indexPath.row]
    return cell

}

根据您提供的代码,您似乎没有在 table 视图中注册您的 table 视图单元格和标识符。

UITableView Class Reference

Discussion Prior to dequeueing any cells, call this method or the registerNib:forCellReuseIdentifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.

If you previously registered a class or nib file with the same reuse identifier, the class you specify in the cellClass parameter replaces the old entry. You may specify nil for cellClass if you want to unregister the class from the specified reuse identifier.

我建议您在 viewDidLoad() 中注册它们。

    let KDepartmentCellIdentifier = "department_cell"
    tableView.registerClass(UITableViewCell.class, forCellReuseIdentifier: kKDepartmentCellIdentifier)

    let KOfficeCellIdentifier = "office_cell"
    tableView.registerClass(UITableViewCell.class, forCellReuseIdentifier: kOfficeCellIdentifier)

如果您使用的是笔尖,则使用

registerNib(_ nib: UINib?, forCellReuseIdentifier identifier: String)