Zeeshan Shabbir
Zeeshan Shabbir

Reputation: 7114

How to assign custom class to TableViewController

I am an android developer and I recently started learning Swift. I facing a problem which I am unable to assign a custom class to UITableView in the UIStoryboard. When I write the class name and hit enter then iMac beeps. I have stuck to this problem form hours. Can someone guide me here?

enter image description here

Here is the class

import Foundation
import UIKit
class MemeTableViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    var memes : [Meme]!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        memes = appDelegate.meme
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "tablecell")!
        let meme = memes[(indexPath as NSIndexPath).row]
        cell.imageView?.image = meme.memeImage
        cell.textLabel?.text = "\(meme.topText)....\(meme.bottomText)"
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let controller = self.storyboard?.instantiateViewController(withIdentifier: "MemeDetailViewer") as! MemeDetailViewer
        let meme = memes[(indexPath as NSIndexPath).row]
        controller.memeImage = meme.memeImage
    }
}

Thanks!

Upvotes: 3

Views: 904

Answers (1)

Anurag Sharma
Anurag Sharma

Reputation: 4824

Before you give the Class name in your UIStoryboard then, Your class should be the sub class of that controller like here: UITableViewController And what you are assigning is a sub class of UIViewController.

Upvotes: 3

Related Questions