Xcode 初学者在简单的 tableview 代码上出现运行时错误 [英] Xcode beginner having run time error on simple tableview code

查看:21
本文介绍了Xcode 初学者在简单的 tableview 代码上出现运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Simon NG Swift 编程指南学习如何使用基于表格的应用程序.我逐字输入代码,Xcode 环境卡在 let cell = tableView 代码行上.

I am learning how to use a table based app using Simon NG Swift Programming guide. I typed in the code verbatim and the Xcode environment gets stuck on the let cell = tableView line of code.

谁能告诉我我做错了什么?

Can someone tell me what I am doing wrong?

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


    var restaurantNames = ["Cafe Deadend", "homei", "teakha", "cafe loius", "petite oyster", "royal oak", "for knee rest",
        "jimmy johns", "mickey dee", "daddies big burgers"]



    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return restaurantNames.count
    }

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

        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell

        // configure the cell
        cell.textLabel?.text = restaurantNames[indexPath.row]
        return cell
    }



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


}

推荐答案

我在运行你的代码时看到的第一个错误是:

The first error I saw when running your code is this:

2015-02-17 16:28:05.645 delete-me-maps[8008:151860] *** Terminating app due 
to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to 
dequeue a cell with identifier Cell - must register a nib or a class for the 
identifier or connect a prototype cell in a storyboard'

如果这是您得到的错误,那么您需要将以下内容添加到 viewDidLoad:

If this is the error you're getting, then you need to add the following to viewDidLoad:

if let myTableView = self.tableView {
    self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
} 

否则 dequeueReusableCellWithIdentifier 不知道该单元格使用什么样的类.

Otherwise dequeueReusableCellWithIdentifier does not know what kind of class to use for the cell.

这篇关于Xcode 初学者在简单的 tableview 代码上出现运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆