0

I have made a ViewController, in that ViewController, I have made a TableView , in the TableView I have added a TableViewCell. In the TableViewCell, I have added a text label and an image. Iv'e made a cocoa touch file named customCell and connected it with the TableView Cell.

In the ViewController file I wrote this:

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

            let cell:customCell = tableView.dequeueReusableCellWithIdentifier("cell") as! customCell
            cell.imageCountry.image = UIImage(named: "2xd.png")
            cell.nameCountry.text = "hey"
return cell

This is what i wrote in the customCell file: import UIKit

class customCell: UITableViewCell {

    @IBOutlet var imageCountry: UIImageView!
    @IBOutlet var nameCountry: UILabel!

}

Iv'e connected the TableView & the ViewController with the DataSource & Delegate. When I run my code this is the error I get:

Could not cast value of type 'UITableViewCell' (0x10d1119f0) to 'refreshing.customCell' (0x10b2f6dd0).

*refreshing is the name of the project.

And the green line of the bugger is set to this line:

let cell:customCell = tableView.dequeueReusableCellWithIdentifier("cell") as! customCell

Any suggestions?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Eliko
  • 1,137
  • 4
  • 16
  • 26
  • Hope this would help: http://stackoverflow.com/questions/29812168/could-not-cast-value-of-type-uitableviewcell-to-appname-customcellname – Niraj Aug 03 '15 at 14:17
  • @Rambo Tried both answers in the link, I get another error in the AppDelegate.swift file in this line: class AppDelegate: UIResponder, UIApplicationDelegate – Eliko Aug 03 '15 at 14:24
  • It sounds like the cell associated with the "cell" identifier isn't defined as a `customCell`. When you say "connected it with the TableView Cell", how did you actually do that? – Phillip Mills Aug 03 '15 at 16:28
  • @PhillipMills In the MainStoryboard>ViewController>Table View>Cell>Identifier:"cell" – Eliko Aug 03 '15 at 16:40
  • Did you also set the cell's class to be `customCell` in the storyboard? – Phillip Mills Aug 03 '15 at 16:58
  • @PhillipMills Yes Phillip. Did I miss here something I have done wrong? – Eliko Aug 03 '15 at 16:59
  • Make sure you've set the cell's class in the Identity Inspector tab in the Utilities menu (right menu) and that your custom class subclasses UITableViewCell – jrisberg Aug 03 '15 at 18:08
  • @jrisberg can you show an example code please? – Eliko Aug 03 '15 at 18:28
  • @Eliko see the answer below – jrisberg Aug 03 '15 at 18:38

2 Answers2

0

You need to set your custom class as the cell's class here

You need to set your custom class as the cell's class here ^

And to subclass, ensure in your CustomClass.h file you have

@interface CustomClass : UITableViewCell

jrisberg
  • 764
  • 4
  • 16
0

you are using the old decking method, that either rquires you to create the cell or use the new that takes the indexPath

let cell:customCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as! customCell

if it is still failing, you need to register the cell, either by setting it up in the storyboard or by registering either a class or a nib file in code.

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.registerNib(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: "cell")
}

or

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.registerClass(customCell.self, forCellReuseIdentifier: "cell")
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • before you edited, I added this line: `tableView.registerNib(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: "customCellOne")` And it worked! I wanted to ask why, because when i type `cell` it has error (like you edited) – Eliko Aug 03 '15 at 19:07
  • the error is: `Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle:` and then a lot of lines of errors.. why does it work when i put an identifier which is not named "**cell**"? – Eliko Aug 03 '15 at 20:31
  • sure u didn't once use registerNib and once registerClass? – vikingosegundo Aug 03 '15 at 22:22
  • Before using Nib I used Class but when I used Nib I did `//` to the Class. Why is it matter? – Eliko Aug 03 '15 at 22:24