1

I created a custom class for a cell in my program. When I try to use it in another class to create a cell I keep getting the error Cannot invoke 'init' with an argument list of type '(style: UITableViewCellStyle, reuseIdentifier: StringLiteralConvertible)'. Can anybody point me in the right direction here? I would really appreciate any help. I tried changing the class to inherit form UITableViewController so I can use this var cell: bookCell = self.tableView.dequeueReusableCellWithIdentifier("cell1") as bookCell but it crashes the program if I try to make the class inherit from tableviewcontroller.

import UIKit

class bookCell: UITableViewCell {

    @IBOutlet var bookImage: UIImageView!

    @IBOutlet var bookDescription: UILabel!

    @IBOutlet var bookPosterUsername: UILabel!
}


 import UIKit

    class SubjectBooksViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


        @IBOutlet var navigationBarTitle: UINavigationBar!

        override func viewDidLoad() {
            super.viewDidLoad()

            self.navigationBarTitle.topItem?.title = "\(selectedCourse)"
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }


         func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return 3

        }

         func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

            return 100

        }

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


        var cell : bookCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "subjectCell")

            //var cell: bookCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "subjectCell")
            //var cell: bookCell = self.tableView.dequeueReusableCellWithIdentifier("cell1") as bookCell

            return cell
        }



    }
3
  • You should be using dequeueReusableCellWithIdentifier to create your cell. You don't need to inherit from UITableviewController to use that method. If that doesn't work, then update you question to show the code you have in your custom cell class.
    – rdelmar
    Commented Mar 24, 2015 at 1:40
  • I tried using dequeueReusableCellWithIdentifier but that gives me an error saying '(UITableView, numberOfRowsInSection: Int) -> Int' does not have a member named 'dequeueReusableCellWithIdentifier' Commented Mar 24, 2015 at 2:05
  • I've seen that error before too; it's not really about that. I think you probably have a missing"!" somewhere in that line or one above. Also, you should call that method on tableView (the one passed in), not on self.tableView.
    – rdelmar
    Commented Mar 24, 2015 at 3:11

2 Answers 2

1

Update code:

import UIKit

class SubjectBooksViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet var navigationBarTitle: UINavigationBar!
    @IBOutlet var myTableView: UITableView! //Outlet for your table View 
    override func viewDidLoad() {
        super.viewDidLoad()
        self.myTableView.dataSource = self //If you have not done in IB
        self.myTableView.registerNib(yourCellNib, forCellReuseIdentifier: "subjectCell")

        self.navigationBarTitle.topItem?.title = "\(selectedCourse)"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 3
    }

     func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 100
    }

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

    var cell : bookCell = tableView.dequeueReusableCellWithIdentifier("subjectCell", forIndexPath: indexPath)

        return cell
    }
}

In viewDidLoad() in line :

self.myTableView.registerNib(yourCellNib, forCellReuseIdentifier: "subjectCell")

replace yourCellNib with loaded nib file for your custom cell.

Registering your nib file is required if you plan to reuse cell in your table view. It is always a good idea to reuse cells.

1

You need to override this function and get your custom cell in this manner:

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

    var cell = tableView.dequeueReusableCellWithIdentifier(
        "subjectCell", 
        forIndexPath: indexPath) as bookCell

Your class name really should be BookCell though, with an uppercase "B". Just to keep with existing standards

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.