【发布时间】: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