![](/img/trans.png)
[英]Set UITableViewCell selected to YES in willDisplayCell: and the cell can't be deselected anymore
[英]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
有什么建议么?
您正在使用旧的装饰方法,该方法要么要求您创建单元格,要么使用采用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.