繁体   English   中英

无法将UITableViewCell设置为单元格

[英]can't set UITableViewCell as a cell

我做了一个ViewController ,在那个ViewController ,我做了一个TableView ,在TableView我添加了TableViewCell TableViewCell ,我添加了文本标签和图像。 我制作了一个名为customCell的可可触摸文件,并将其与TableView Cell连接。

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

这是我在customCell文件中写的:import UIKit

class customCell: UITableViewCell {

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

}

我将TableView和ViewController与DataSource和Delegate连接起来。 当我运行代码时,这是我得到的错误:

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

*刷新是项目的名称。

Bugger的绿线设置为此行:

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

有什么建议么?

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

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

要进行子类化,请确保在CustomClass.h文件中

@interface CustomClass : UITableViewCell

您正在使用旧的装饰方法,该方法要么要求您创建单元格,要么使用采用indexPath的新方法

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

如果仍然失败,则需要通过在情节提要中进行设置或通过在代码中注册类或nib文件来注册单元。

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

要么

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