![](/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.