Create a UITableViewController programmatically in Swift

I am trying, as the name says, to programmatically configure the UITableViewController. After several hours of trying, I hope someone can help me. And, yes, I checked other posts on this:

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 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 } 

Running the program, but as soon as this happens, I get the following error:

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

Then I change the value of 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 greatly appreciated

+7
uitableview ios8 swift programmatically-created
source share
3 answers

I use the UITableViewController subclass without any problems using the (nibName: bundle :) form, but I overridden this in my subclass. I tried replacing my subclass with a standard UITableViewController, and it still worked fine. Perhaps you are overriding the init (...) method in your subclass?

+3
source share

In AppDelegate or wherever you call 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 (in the absence of a custom implementation of TableViewCell):

 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 a complete example: visit: https://github.com/ericcgu/EGStormTracker

+3
source share

I just delete

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

and then follow these steps:

  let mainViewController: UITableViewController = MainViewController() 
0
source share

All Articles