22

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 ?

Sebastian
  • 5,137
  • 5
  • 29
  • 47

4 Answers4

15

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.

Sulthan
  • 118,286
  • 20
  • 194
  • 245
  • I had to adjust other things in cellForRowAtIndexPath, but now it's working. Thanks Sulthan ! – Sebastian Jun 03 '14 at 21:28
  • What did you adjust? I assume you should be using 'tableView.dequeueReusableCellWithIdentifier("reuseIdentifier")' because you are not using xib's for cells are you? – tafh Jun 04 '14 at 19:08
  • 1
    This has been finally fixed in Swift 2, as of XCode 7.0 beta (7A120f). One can use super.init(style: style) directly now… – Aymeric Barthe Jun 19 '15 at 06:58
10

Instead of

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

you might find this handy:

convenience init() {
    self.init(style: .Plain)
    title = "Plain Table"
}

Then, you can just call TableViewController() to initialize.

ma11hew28
  • 106,283
  • 107
  • 420
  • 616
  • In Xcode 6 beta 5, you can no longer declare a no-argument convenience initializer in this case. See: http://stackoverflow.com/questions/25139494/how-to-subclass-uitableviewcontroller-in-swift – Nick Snyder Aug 05 '14 at 13:15
  • Been looking for this for an hour now. Thank you so much!! Works on Xcode 7.0 & Swift 2.0 – Kevin van Mierlo Sep 27 '15 at 21:23
3

It is as simple as writing a function

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")

    cell.text = self.Myarray[indexPath.row]
    cell.textLabel.textColor = UIColor.greenColor()

    cell.detailTextLabel.text = "DummyData #\(indexPath.row)"
    cell.detailTextLabel.textColor = UIColor.redColor()
    cell.imageView.image = UIImage(named:"123.png")
    return cell
}
ANIL.MUNDURU
  • 373
  • 3
  • 2
  • cell.text is deprecated and unavailable: `'text' is unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift` – djbp Dec 08 '14 at 16:02
1

Cell Function Use:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{

    var cell = tableView.dequeueReusableCellWithIdentifier(kLCellIdentifier) as UITableViewCell!
    if !cell {
        cell = UITableViewCell(style:.Default, reuseIdentifier: kLCellIdentifier)
    }
    cell.backgroundColor = UIColor.clearColor()
    cell.textLabel.text = arrData[indexPath.row]
    cell.image = UIImage(named: "\(arrImage[indexPath.row])")   
    cell.accessoryType  = UITableViewCellAccessoryType.DetailDisclosureButton
    cell.selectionStyle = UITableViewCellSelectionStyle.None
    return cell
}
Nagarjun
  • 5,811
  • 5
  • 28
  • 47