2015-09-22 45 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