Jude Molloy
Jude Molloy

Reputation: 193

Unable to set a custom class for UITableViewController in Xcode

Xcode will not let me set the custom class of my table view to the following class(when I go to set it on the storyboard it does not let me and nothing comes up on the drop down).

The following is my swift file for my table view controller.

https://gist.github.com/JudeMolloy/139e91d8c45ebe3d6140139ffd68a339

Here is a picture of my storyboard: enter image description here

Thanks.

Upvotes: 3

Views: 2955

Answers (3)

Fahmi Al
Fahmi Al

Reputation: 1

If you want to set a custom class on a controller, the custom class you want to choose should have inheritance of that controller, in this case, UITableViewController.

Let's say you want to use ViewController as a controller custom class, set its parent to UITableViewController.

import UIKit

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


}

Then you can see ViewController in the custom class dropdown at inspector tab:

select custom class

Upvotes: 0

shadowsheep
shadowsheep

Reputation: 15002

Check If your class has at least these methods:

//
//  MyCustomTableViewController.swift
//  Test Table
//
//  Created by shadowsheep on 17/02/2018.
//  Copyright © 2018 shadowsheep. All rights reserved.
//

import UIKit

class MyCustomTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

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

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }

}

With these basic stub you shold be able to access your controller for a UITableViewController for sure...

Edit From the new info provided: You must select the UITableViewController and not the UiTableView. Once selected your UITableViewController yoy will be able to set your custom controller.

enter image description here

You'll see you have selected the right class because the light grey default class provided on the Identity Inspector is UITableViewController

Than you can find your custom controller in the dropdown menu.

enter image description here

Upvotes: 7

Mike Taverne
Mike Taverne

Reputation: 9352

In the picture you provided, you have selected the UITableView, not the view controller.

You cannot set the custom class of a UITableView to a view controller.

Make sure you select the view controller in the storyboard. Click the yellow icon in the bar above your view controller. Then you will be able to set the custom class.

Upvotes: 2

Related Questions