7

I'm trying to, as the title say, set up a UITableViewController programmatically. After a few hours of trying I hope someone can help me. And, yes, I hve checked out other posts on this matter:

import UIKit

class MainViewController: UITableViewController {

    init(style: UITableViewStyle) {
        super.init(style: style)
        // Custom initialization
    }

    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // #pragma mark - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
        return 5
    }


    override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
        var cell = tableView?.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell

        if !cell {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
        }
        cell!.textLabel.text = "test"
        return cell
    }

}

and the appDelegate looks like this:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

        let mainViewController: UITableViewController = MainViewController(style: UITableViewStyle.Plain)
        let navigationController: UINavigationController = UINavigationController()
        navigationController.pushViewController(mainViewController, animated: false)

        self.window!.rootViewController = navigationController
        self.window!.backgroundColor = UIColor.whiteColor()
        self.window!.makeKeyAndVisible()
        return true
    }

The program run, but as soon as it does, I get the following error:

fatal error: use of unimplemented initializer 'init(nibName:bundle:)' for class 'HelloWorld.MainViewController'

I then changes the MainViewController(style: UITableViewStyle.Plain) to MainViewController(nibName: nil, bundle: nil) but then I get the following syntax error: Extra argument 'bundle' in call

Any help would be much appreciated

2
  • How are you defining the MainViewController class? Commented Jun 7, 2014 at 15:41
  • What's the definition for MainViewController? Are you inheriting directly from UITableViewController? Do you define any initialisers? Commented Jun 7, 2014 at 15:42

3 Answers 3

3

I'm using a subclass of UITableViewController with no issues, using the (nibName:bundle:) form, but I've overridden that in my subclass. I tried replacing my subclass with a standard UITableViewController, and it still worked fine. Are you possibly overriding an init(...) method in your subclass?

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

3 Comments

When I removed the init function in MainViewController the program run just fine. I just came over a post here on SO with the same problem (don't know how I didn't see that post before posting..). But why is this necessary?
make sure you're calling the appropriate inherited init(...) method from your own initializer.
Swift makes inherited initializers inaccessible for clients of your derived class when you add a designated initializer. It looks similar to private inheritance in C++ where the inherited methods/data are implicitly moved to the "private" class section. The reason is simple: if there is a custom initializer then your class probably requires this initializer to construct class "invariant". But there is a bug: if you create class derived from UITableViewController and add custom initializer that calls super.init(style:) it will fail with use of unimplemented initializer 'init(nibName:bundle:)'
3

In the AppDelegate or wherever you call the TableViewController:

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    if let window = window {
      window.backgroundColor = UIColor.whiteColor()
      var mainTableViewController = MainTableTableViewController(style: .Plain)
      window.rootViewController = UINavigationController(rootViewController: mainTableViewController)
      window.makeKeyAndVisible()
    }
    return true
}

In the UITableViewController (assuming no custom TableViewCell implementation):

import UIKit

class MainTableTableViewController: UITableViewController {

  override init(style: UITableViewStyle){
    super.init(style: style)
  }

  override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!){
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  }

  required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")

  }

  // MARK: - Tableview Data Source

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return [*YOUR_DATA_ARRAY].count
  }

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel!.text = [*YOUR_DATA_ARRAY][indexPath.row]
  }

For an full example: visit: https://github.com/ericcgu/EGStormTracker

Comments

0

I just removing

init(style: UITableViewStyle) {
    super.init(style: style)
}

and then init like this:

 let mainViewController: UITableViewController = MainViewController()

Comments

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.