简体   繁体   中英

Swift - UITableView 'unable to dequeue a cell with identifier Cell'

I am working on a project for iOS in Swift. This app involves displaying JSON data in a UITableView.

I have the JSON Data parsed and all that but its not showing up on the tableview. When ever i run my app it gives me this error:

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'

I am trying to do everything programmatically instead of using the storyboards. I will add my code below.

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

Can anyone give me a hand with this and show me where I'm going wrong?

Thanks!

Make sure that somewhere (probably in TableViewController 's viewDidLoad ), you register the cell class:

Swift 4:

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

Swift 3 and below:

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

Please use:

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

in viewDidLoad func and tell us the result.

在Swift 4中使用register而不是registerClass

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM