2015-09-22 125 views
0

我只是试图在我设置的Smiles数组中显示名称的tableview。我需要将我的单元类“ClubCell”声明为扩展到UITableViewCell,我将如何去做这件事?声明我的CellClass扩展到UITableViewCell?

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 
} 
+0

为什么您只是完全改变了您的问题?不要这样做。它将现有的答案留在不好的地方。我只是说你应该在你的问题中添加一行,而不是删除原始问题的详细信息。 – rmaddy

+0

顺便说一句 - 查看我更新的答案。仅供参考 - 如果您不知道如何在Swift中扩展课程,那么您应该回到Swift书籍并研究基础知识以供审阅。 – rmaddy

回答

2

cellForRowAtIndexPath方法的返回值需要是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

它应该是:

class ClubCell : UITableViewCell 
+0

当我实施您的建议时,我在“退货单元”行上收到错误消息。 错误信息:无法将类型'ClubCell'的返回表达式转换为返回类型'UITableViewCell' – pmoney13

+1

您的'ClubCell'类是否扩展了'UITableViewCell'? – rmaddy

+0

我对此有点新,并且不确定你在问什么。 – pmoney13

0

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] as String没有必要垂头丧气到字符串,因为你的静态数组已转换为字符串,因此将其更改为cell.Name.text = self.Smiles[indexPath.row]

+0

哇我不是...我的朋友 – Lamar

+0

至少解释你的答案,什么是错误的,你的答案是否解决了这个问题,只有代码的答案在这里皱起了眉头 – rmaddy

+0

当我在“return cell”行出现错误时我实现了你的建议,和上面一样。 错误如下:无法将类型'ClubCell'的返回表达式转换为返回类型'UITableViewCell' – pmoney13