简体   繁体   中英

SWIFT: unable to dequeue a cell with identifier cell error when displaying a dictionary in a table view with multiple sections

I'm trying to a guest list for multiple events in a Table View. See storyboard below -

在此处输入图片说明

I've created four sections, and have named all of the cell identifiers "cell". When I run the app, I get the following 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'

Here's my code:

import UIKit

class RSVPTableViewController: UITableViewController {

var names = ["Event 1": ["Guest 1", "Guest 2", "Guest 3"], "Event 2": ["Guest 1", "Guest 2"]]

struct Objects {

    var sectionName : String!
    var sectionObjects : [String]!
}

var objectArray = [Objects]()

override func viewDidLoad() {
    super.viewDidLoad()

    for (key, value) in names {
        print("\(key) -> \(value)")
        objectArray.append(Objects(sectionName: key, sectionObjects: value))
    }

}

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 Incomplete implementation, return the number of sections
    return objectArray.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return objectArray[section].sectionObjects.count
}


override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    return objectArray[section].sectionName
}


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

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    cell.textLabel?.text = objectArray[indexPath.section].sectionObjects[indexPath.row]

    return cell


}

}

Static Cells are intended for...well... static content. Given that you have a dynamic array you want to use to set cell content, you should use the Dynamic Prototypes cell type.

After giving the prototype cell (you should only need one) an identifier, you'll be good to go.

You should use Dynamic types as you actually need a custom cell to use your array. please do select Prototype from your tableview's attribute Content. and make your custom 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