Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically UITableView using swift

Tags:

I'm trying to create a simple tableView programmatically using swift, so I wrote this code on "AppDelegate.swift" :

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {     self.window = UIWindow(frame: UIScreen.mainScreen().bounds)      var tvc :TableViewController = TableViewController(style: UITableViewStyle.Plain)     self.window!.rootViewController = tvc      self.window!.backgroundColor = UIColor.whiteColor()     self.window!.makeKeyAndVisible()     return true     } 

Basically I added the TableViewController creation and added to the window. And this is the TableViewController code:

class TableViewController: UITableViewController {  init(style: UITableViewStyle) {     super.init(style: style)  }  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 10 }   override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {     var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell      cell.textLabel.text = "Hello World"      return cell } 

}

So, when I try to run the code I receive this message:

Xcode6Projects/TableSwift/TableSwift/TableViewController.swift: 12: 12: fatal error: use of unimplemented initializer 'init(nibName:bundle:)' for class 'TableSwift.TableViewController'

The error occurs when the compiler is executing the

super.init(style: style)

Any thoughts ?

like image 298
Sebastian Avatar asked Jun 03 '14 19:06

Sebastian


People also ask

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

Does SwiftUI have Tableview?

SwiftUI's List view is similar to UITableView in that it can show static or dynamic table view cells based on your needs.


1 Answers

In Xcode 6 Beta 4

Removing

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

will do the trick. This is caused by different initializer behavior between Obj-C and Swift. You have created a designated initializer. If you remove it, all initializers will be inherited.

The root cause is probably in -[UITableViewController initWithStyle:] which calls

[self initWithNibName:bundle:] 

I actually think this might be a bug in the way Obj-C classes are converted to Swift classes.

like image 121
Sulthan Avatar answered Sep 24 '22 22:09

Sulthan