简体   繁体   中英

can't set UITableViewCell as a cell

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?

您需要在此处将自定义类设置为单元格的类

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

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")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM