Reputation: 44310
I have the following single view app. The tablview delegate/datasource are connected to the Root View Controller through IB. When the app loads, the tableview is empty. I've set a breakpoint in all methods of the ViewController.swift file and nothing is hit.
I have added the following in ViewController.swift (UITableViewDataSource is also there now):
UITableViewDelegate
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "test")
cell.textLabel?.text = "just a cell"
return cell;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
Since no breakpoints are getting hit in ViewController.swift, I think something is still not connected. Any ideas what this might be or what I'm doing wrong?
The project has just the stock Single View app files:
-- EDIT --
Cell identifier updated but still no data in the table.
Below is the connection from the tableveiw to the Root View Controller.
Upvotes: 0
Views: 1850
Reputation: 14918
Looks like you have 2 problems:
When your Root View Controller is selected, what appears here in the right hand pane? It should be ViewController (the name of your ViewController class) like this:
Note that IB won't let you select a class which is not a UITableViewController
subclass. If you have an empty Storyboard and drag out a Navigation Controller, the Root View Controller it creates is a Table View Controller, and it will only let you associate that with a UITableViewController
subclass. If you really don't want to use a UITableViewController
you have to delete the Root View Controller that IB created for you, drag out a regular View Controller, add in the Table View and then hook everything up as necessary by hand.
The way you've hooked up the outlets doesn't look right. The UITableView
should have outlets dataSource
and delegate
referencing the RootViewController. Like this:
Whereas the RootViewContoller should have referencing outlets dataSource and delegate referencing the Table View.
From the the crash you're getting ([UINavigationItem tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
) it's clear that you have hooked up the dataSource and delegate to the navigation bar, not the View Controller (the yellow circle Storyboard hierarchy view).
Upvotes: 2
Reputation: 90117
As you can see in the cell inspector the style of your prototype cell is "Custom". Such a cell does not have a textLabel. Switch the style to Default and you will get a UITableViewCell that has a textLabel
.
And since it's obviously an error if there is no textLabel you should replace textLabel?
with textLabel!
:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "test")
cell.textLabel!.text = "just a cell"
return cell;
}
Upvotes: 0
Reputation: 15669
Check the Identifier in Storyboard - it should say test where I have switchCell. You'll find it in the bar on the right side by clicking on the prototype cell:
Upvotes: 0