简体   繁体   English

无法将UITableViewCell设置为单元格

[英]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 . 我做了一个ViewController ,在那个ViewController ,我做了一个TableView ,在TableView我添加了TableViewCell In the TableViewCell , I have added a text label and an image. TableViewCell ,我添加了文本标签和图像。 Iv'e made a cocoa touch file named customCell and connected it with the TableView Cell. 我制作了一个名为customCell的可可触摸文件,并将其与TableView Cell连接。

In the ViewController file I wrote this: ViewController文件中,我这样写:

    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 这是我在customCell文件中写的:import UIKit

class customCell: UITableViewCell {

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

}

Iv'e connected the TableView & the ViewController with the DataSource & Delegate. 我将TableView和ViewController与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: Bugger的绿线设置为此行:

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 要进行子类化,请确保在CustomClass.h文件中

@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 您正在使用旧的装饰方法,该方法要么要求您创建单元格,要么使用采用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. 如果仍然失败,则需要通过在情节提要中进行设置或通过在代码中注册类或nib文件来注册单元。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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