Reputation: 145
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
}
}
Upvotes: 0
Views: 783
Reputation: 57
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.
Upvotes: 0
Reputation: 15377
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.
Upvotes: 1