Ios 声明我的CellClass以扩展到UITableViewCell?
我只是尝试在我设置的微笑数组中显示姓名的表视图。我需要声明我的单元类“ClubCell”以扩展到UITableViewCell,我将如何执行此操作Ios 声明我的CellClass以扩展到UITableViewCell?,ios,swift,uitableview,Ios,Swift,Uitableview,我只是尝试在我设置的微笑数组中显示姓名的表视图。我需要声明我的单元类“ClubCell”以扩展到UITableViewCell,我将如何执行此操作 class SmileClub: UITableViewController { var Smiles: [String] = ["Price Garrett", "Michael Bishop", "Tom Kollross", "Cody Crawford", "Ethan Bernath", "Alex Mlynarz", "Ryan Mu
class SmileClub: UITableViewController {
var Smiles: [String] = ["Price Garrett", "Michael Bishop", "Tom Kollross", "Cody Crawford", "Ethan Bernath", "Alex Mlynarz", "Ryan Murphy", "Kelly Murphy", "Ryan Roshan", "Sean Ko"]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> ClubCell
{
let cell: ClubCell = tableView.dequeueReusableCellWithIdentifier("ClubCell") as! ClubCell!
cell.Name.text = self.Smiles[indexPath.row] as String
return cell
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.Smiles.count
}
cellforrowatinexpath
方法的返回值必须是UITableViewCell
,而不是ClubCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: ClubCell = tableView.dequeueReusableCellWithIdentifier("ClubCell") as! ClubCell!
cell.Name.text = self.Smiles[indexPath.row] as String
return cell
}
并确保您的ClubCell
类扩展了UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: ClubCell = tableView.dequeueReusableCellWithIdentifier("ClubCell") as! ClubCell!
cell.Name.text = self.Smiles[indexPath.row] as String
return cell
}
应该是:
class ClubCell : UITableViewCell
您的
ClubCell
是UITableViewCell的一个子类,它应该如下所示:
class ClubCell:UITableViewCell
{
@IBOutlet weak var Name:UILabel!
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("ClubCell",forIndexPath:indexPath) as! ClubCell
cell.Name.text = self.Smiles[indexPath.row]
return cell
}
在这一行中,
cell.Name.text=self.Smiles[indexPath.row]作为字符串
无需向下转换为字符串,因为您的静态数组已经转换为字符串,所以将其更改为cell.Name.text=self.Smiles[indexPath.row]
当我实现您的建议时,“返回单元格”行出现错误。错误如下:无法将“ClubCell”类型的返回表达式转换为返回类型“UITableViewCell”。您的ClubCell
类是否扩展了UITableViewCell
?我对此有点陌生,不确定您在问什么。请使用ClubCell
类的声明更新您的问题(您的ClubCell.swift
文件的class
行。谢谢!这似乎清除了错误,但现在我在选择SmileClub VC时遇到了运行时/编译错误-我遇到了致命的崩溃和错误:“无法将'UITableViewCell'(0x10f43f490)类型的值强制转换为'ToothSense.ClubCell'”哇,我不是…我的朋友至少解释一下你的答案。你的答案解决了什么问题和问题。这里只显示代码答案。我在“返回单元格”上出错当我实现你的建议时,行,如上所述。错误如下:无法将类型为“ClubCell”的返回表达式转换为类型为“UITableViewCell”的返回表达式。为什么你刚刚完全更改了你的问题?不要这样做。这会将现有答案留在一个不好的地方。我只是说你应该在问题中添加一行,而不是删除原始行最后一个问题详细信息。顺便说一句-见我的更新答案。仅供参考-如果你不知道如何在Swift中扩展课程,那么你应该回到Swift手册,学习基础知识以供复习。