Reputation: 9660
I have done this million times in Objective-C but for some reason Swift is not working as expected. The following code does not populate the UITableView cell with one row:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
var cell = tableView.dequeueReusableCellWithIdentifier("BudgetTableViewCell", forIndexPath: indexPath) as BudgetTableViewCell
cell.budgetTitle.text = "My Budget"
return cell
}
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1;
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return 1;
}
I have made sure that the budgetLabel is hooked up properly in Storyboard. All the above methods are invoked but when the UITableView loads there is nothing displayed.
THE UITableViewController Template Apple Provides:
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 0
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return 0
}
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
// Configure the cell...
return cell
}
Upvotes: 0
Views: 821
Reputation: 70135
The UITableView delegate methods have an interface specification slightly different from your override attempts. Here is the interface that the Apple Xcode template code provides as an implementor's starting point (for a Master/Detail project):
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}
Your code uses ?
and !
in an number of places. This means that there is not an override match; instead you are creating newly typed methods. Thus your methods are never being called.
Note: the Apple Xcode templates for a Master/Detail project, a Cocoa Touch Class as a subtype of UITableViewController, and the UITableViewDataSource documentation all use inconsistent signatures as regards UITableView
, UITableView?
and UITableView!
use.
It should, however, be clear that in a delegate method a declaration of UITableView!
should be appropriate as the provided table argument will exist (not be nil
) if the delegate is even called.
Upvotes: 1