【问题标题】:Listing contents using UITableView使用 UITableView 列出内容
【发布时间】:2015-07-29 15:34:44
【问题描述】:

我是 iOS 编程的新手,我只是想设置一个可以填充数组内容的 TableView。 我一直遇到以下错误。

未捕获的异常 'NSInternalInconsistencyException',原因:'-[UITableViewController loadView] 加载了“BYZ-38-t0r-view-8bC-Xf-vdC” nib 但没有获得 UITableView。'

我已经研究了几个建议的解决方案,但没有一个适合我的情况。

有人建议我将我的 ViewController 设为 UIViewController 的子类,但我这样做会得到以下error

另一个建议我在我的自定义类中使用 UIViewController 而不是 UITableViewController 但我得到以下error 这样做。

我的自定义类代码

//
//  AddQuestionTableViewController.swift
//  Quiz Add Question
//
//  Created by Emil Shirima on 7/29/15.
//  Copyright (c) 2015 Emil Shirima. All rights reserved.
//

import UIKit

class AddQuestionTableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {


var sample = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    sample = ["apple","pear","banana","raspberry"]

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return sample.count
}

// indexPath has info about which section and which row
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("questionCell", forIndexPath: indexPath) as! UITableViewCell

    // Configure the cell...
    let data = sample[indexPath.row]
    cell.textLabel?.text = sample[indexPath.row]

    return cell
}

/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the specified item to be editable.
    return true
}
*/

/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        // Delete the row from the data source
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
*/

/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

}
*/

/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the item to be re-orderable.
    return true
}
*/

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
}

我的 ViewController 代码

//
//  ViewController.swift
//  Quiz Add Question
//
//  Created by Emil Shirima on 7/29/15.
//  Copyright (c) 2015 Emil Shirima. All rights reserved.
//

import UIKit

class ViewController: UIViewController{

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

我当前的带有层次结构的故事板可以在这里找到://i.stack.imgur.com/2lEVe.png。 抱歉,没有足够的声望点来发布额外的链接。

我确实对为什么会发生这种情况有一个大致的了解,但如果有人能向我解释为什么以及如何解决它,我将真诚地感谢它。

【问题讨论】:

  • 您的委托和数据源是否已连接到 Storyboard 中的类?
  • 我该如何检查呢?我已将表视图(包括数据存储和委托)链接到视图控制器。

标签: ios iphone swift uitableview uiviewcontroller


【解决方案1】:

请查看 Storyboard 上名为“AddQuestionTableViewController”的 ViewController。

我认为您将“AddQuestionTableViewController”命名为错误的 ViewController(不是 UITableViewController)

【讨论】:

    【解决方案2】:

    造成这种情况的一些常见原因与 Storyboard 连接和 Table View Controller 文件有关。首先,在 Storyboard 中单击 Table View 本身并检查 Outlets。

    如果它们断开连接,从delegatedata source 中的圆圈拖动到视图控制器顶部的小表格视图控制器图标。

    另外,请确保您的课程已正确连接。转到您的 Table View Controller(不是 Table 本身)的故事板中的 ID 页面并检查类。

    【讨论】:

      【解决方案3】:

      错误已告诉你详细信息:[UITableViewController loadView] loaded the "BYZ-38-t0r-view-8bC-Xf-vdC" nib but didn't get a UITableView.

      您应该检查您的自定义 UITableView。

      你的问题可能:

      1. 您没有在 xib 文件中将自定义 UITableView 标识设置为“BYZ-38-t0r-view-8bC-Xf-vdC”。如果是这样,请转到 xxx.xib->show identity inspector->Identity 将恢复 ID 设置为“BYZ-38-t0r-view-8bC-Xf-vdC”。

      2. 您已将 xib 文件的标识设置为“BYZ-38-t0r-view-8bC-Xf-vdC”,但它不是 UITableView,而是 UIView 或其他东西。如果是这样,请将标识设置为自定义 UITableView

      【讨论】:

        猜你喜欢
        • 2010-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-22
        • 2011-12-09
        • 1970-01-01
        • 2019-09-05
        • 1970-01-01
        相关资源
        最近更新 更多