user6638070
user6638070

Reputation:

Unable to make outlet for UITableView

I'm trying to put a tableview inside of a UIViewController but I'm unable to control drag from the UITableView into my view controller to make an outlet. When I try to drag, it refuses to make a prompt for an outlet. I've even tried to make an outlet myself and then drag from the view controller class to the table view but it refuses to link.

Here is my workspace

Here are my classes for the Tableview:

The Season Cell Class

import UIKit

class SeasonCell: UITableViewCell {
     @IBOutlet weak var seasonLabel: UILabel!
}

The Season Class

class Season: NSObject {
    var name: String

    init(name: String){
        self.name = name
    }

}

The View Controller

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var seasons = [Season]()

    //created myself, wont link
    @IBOutlet weak var seasonsTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        seasons.append(Season(name: "2018 Cross Country Season"))
        seasonsTableView.delegate = self
        seasonsTableView.dataSource = self
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return seasons.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "seasonCell", for: indexPath) as? SeasonCell

        // Configure the cell...
        let thisSeason = seasons[indexPath.row]
        cell?.seasonLabel?.text = thisSeason.name

        return cell!
    }


}

Upvotes: 0

Views: 1688

Answers (1)

thecloud_of_unknowing
thecloud_of_unknowing

Reputation: 1205

You need to 1) give the view controller's class a unique name. This in turn creates a type. 2) You then need to open Storyboard, and set the class of the view controller that contains the tableview to be the type you just created. This will allow you to create an Outlet using control drag.

The way to set the type of the view controller in storyboard is as follows:

enter image description here

Upvotes: 1

Related Questions