2017-01-07 24 views
0

時,我的the main storyboard設置爲table viewprototype cellidentifier設置爲「單元格」。表格視圖有data source outletdelegate outlet
但是,當我在第11行創建表視圖時(見下面的代碼),我的構建每次都會因爲「未解析的標識符」而崩潰。TableViewCell未解決的標識符在我的Xcode項目中創建單元格

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 
{ 

let array = createArray() 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return(validDeck.count) 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    cell = UITableViewCell(style: UITableViewStyle.default, reuseIdentifier: "cell") |"ERROR: Unknown Identifier 'cell' " 
    cell.textlabel?.text = "" 
    return cell 
} 

有沒有人知道這個錯誤,並願意幫忙?

+1

不要使用括號,這不是一個功能,例如'return validDeck.count' – vadian

回答

2

使用下面的代碼迅速3. *

`return`後
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return validDeck.count // assuming count must be greater then 0 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell 

cell.textlabel?.text = "Cell tittle" 
    return cell 
} 
+0

mTab​​leView '代表我的ViewController沒有mTableView的屬性... – Narusan

+1

@Narusan刪除':UITableViewCell'(無用的註釋,編譯器可以推斷出類型)並用'tableView.'代替'self.mTableView.'。 **表視圖實例作爲第一個參數傳遞 – vadian

+0

@vadian你可以編輯來澄清你的意思嗎?我不太明白,如果我刪除self.mTableView,唯一的那剩下就是出列電話... – Narusan

1

您從不直接初始化UITableViewCell實例。

viewDidLoad

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") 

然後:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for indexPath: indexPath) 
    cell.textlabel?.text = "" 
    return(cell) 
} 
+0

我還是得到了一個2錯誤:'在viewDidLoad和'cell = tableView.dequeueReusableCell中成員'tableview_(numberOfRowsInSection)'的歧義引用(wi ...'也行不通,Xcode不接受對於...我使用的是Swift3,這可能是其原因嗎? – Narusan

+0

如果在Interface Builder中設計它們,根本不需要註冊單元格 – vadian

+1

@Narusan oops我忘了let,現在編輯 –

相關問題