0

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
    }
3
  • 2
    john doe, I don't think you're providing enough information to answer your question. For instance, did you register a class for that reuse identifier? Did you configure the delegate? Did you set a debug breakpoint to verify the code is called? Commented Jun 30, 2014 at 21:14
  • 1
    I debugged and the code is being called. I registered the class using Storyboard for the UITableViewCell. Commented Jul 1, 2014 at 15:52
  • Interesting! Perhaps the issue is in the BudgetTableViewCell class? Are there any layout warnings sent to the console? Dumping the view hierarchy might shed some light as well. Commented Jul 2, 2014 at 19:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! I actually debugged the application and the methods are being called.
Where did you get the above methods? If I jump into the source I get the methods with ? and ! all over the place.
Please see my updated question where I have posted the exact template that Apple provides.
Using 'Xcode-Beta6, Version 6.0 (6A215l)' by creating a new iOS project as a Master/Detail Application.
I am now using Beta 2 6A216f using Swift and having the same exact issue. I used the Single View Application Template.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.