Swift - UITableView'无法使用标识符Cell将单元格出列'

ADu*_*gan 2 json uitableview ios swift

我正在为Swift中的iOS项目工作.此应用程序涉及在UITableView中显示JSON数据.

我已经解析了JSON数据,但它没有显示在tableview上.当我运行我的应用程序时,它给了我这个错误:

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法使用标识符Cell将单元格出列 - 必须为标识符注册一个nib或类或连接一个storyboard中的原型单元格'

我试图以编程方式执行所有操作,而不是使用故事板.我将在下面添加我的代码.

import UIKit
import Foundation

class TableViewController: UITableViewController
{
    var postsCollection = [Post]()
    var service: PostService!

    override func awakeFromNib()
    {
        super.awakeFromNib()
    }

    override func viewDidLoad()
    {
        super.viewDidLoad()

        service = PostService()
        service.getPosts
        {
            (response) in 
            self.loadPosts(response["Events"]! as! NSArray)
        }
    }  

    func loadPosts(events: NSArray)
    {
        for event in events
        {
            //var event = event["Events"]! as! NSDictionary
            print("\n-----------------------------------------------------")
            print("           RECEIVED_JSON_DATA")
            print("-----------------------------------------------------")
            print("Title              :   \(event["Title"])")
            print("Event Description  :   \(event["EventDescription"])")
            print("Event Location     :   \(event["EventLocation"])")
            print("Event StartTime    :   \(event["EventStartTime"])")
            print("Event EndTime      :   \(event["EventEndTime"])")
            print("RowKey             :   \(event["RowKey"])")
            print("-----------------------------------------------------\n")

            var title = event["Title"]! as! String
            var description = event["EventDescription"]! as! String
            var location = event["EventLocation"]! as! String
            var startTime = event["EventStartTime"]! as! String
            var endTime = event["EventEndTime"]! as! String
            //var eventURL = event["EventURL"]! as! String
            var rowKey = event["RowKey"]! as! String

            //var postObj = Post(title: title, description: description, location: location, startTime: startTime, endTime: endTime, eventURL: eventURL, rowKey: rowKey)
            var postObj = Post(title: title, description: description, location: location, startTime: startTime, endTime: endTime, rowKey: rowKey)

            postsCollection.append(postObj)
        }

        dispatch_async(dispatch_get_main_queue())
        {
            self.tableView.reloadData()
        }
    }

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

    // TableView
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        return 1
    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        let object = postsCollection[indexPath.row]

        cell.textLabel!.text = object.title

        return cell
    }

    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
    {
        // Return false if you do not want the specified item to be editable.
        return true
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我一把,告诉我哪里出错了?

谢谢!

Bal*_*Ben 10

确保在某处(可能是在TableViewControllers中viewDidLoad),您注册了单元类:

斯威夫特4:

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
Run Code Online (Sandbox Code Playgroud)

斯威夫特3及以下:

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
Run Code Online (Sandbox Code Playgroud)