ios - swift -UITableView 'unable to dequeue a cell with identifier Cell'

标签 ios json swift uitableview

我正在使用 Swift 开发一个 iOS 项目。此应用涉及在 UITableView 中显示 JSON 数据。

我已经解析了 JSON 数据,但它没有显示在表格 View 中。每当我运行我的应用程序时,它都会给我这个错误:

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'

我正在尝试以编程方式完成所有操作,而不是使用 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
    }
}

谁能帮我解决这个问题并告诉我哪里出错了?

谢谢!

最佳答案

确保在某处(可能在 TableViewControllerviewDidLoad 中)注册单元格类:

swift 4:

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")

Swift 3 及以下版本:

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")

关于ios - swift -UITableView 'unable to dequeue a cell with identifier Cell',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36150011/

相关文章:

ios - ARKit 在赛道上小跳

objective-c - 复制 UIImageView 子类

ios - 无法将类型 'Swift.Array<Swift.Dictionary<Swift.String, Swift.String>>' () 的值转换为 'Swift.Dictionary<Swift.String, Any>' ()

ios - 在 parse.com 的 ios 中使用 google plus 登录

ios - counter++/counter--未按预期工作

c# - 在 Windows Phone 中解析 JSON 失败

java - 使用java struts 2在amcharts中加载动态数据

string - 将数字格式化为带有千位分隔符的字符串,并且在 swift 中小数点后始终有 2 位数字

Swift Firebase : UIRefreshControl with . 子添加

ios - 将信息传递给模态视图 Controller