作者whitefox0102 (一個人踢球的小正)
看板MacDev
標題[問題] tableView調用多種Cell
時間Thu Jan 4 11:15:34 2018
各位大大好
小弟想在一個View上面使用一個tableview
並且在這個tableview上的不同section使用不同的的自定義Cell
不過在設定好之後我無法在cellForRowAt這個func裡面調用該Cell的變數
會出現"Value of type 'UITableViewCell' has no member 'plusLabel'"的錯誤
在customCell裡面也有建立一個customCell1.swift並且有IBOutlet做連結
cell是直接在tableview上面建立的,沒有另外建立xib
如圖
https://imgur.com/MWSVCNe
請問是哪裡有問題呢?
是在ustomCell的檔案裡面漏了什麼嗎?
下面貼上代碼
public func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
var cell :UITableViewCell!
switch indexPath.section {
case 0:
cell = tableView.dequeueReusableCell(withIdentifier: "Cell1",
for: indexPath) as! customCell1
cell.plusLabel?.text = "" //<======這裡出現錯誤
case 1:
cell = tableView.dequeueReusableCell(withIdentifier: "Cell2",
for: indexPath) as! customCell2
cell.textLabel?.text = seceion4[indexPath.row]
default:
break
}
return cell
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 119.14.46.198
※ 文章網址: https://www.ptt.cc/bbs/MacDev/M.1515035741.A.202.html
推 tentenlee: 你的cell宣告是UITabeleViewCell雖說你把另外一個custo 01/04 12:04
推 keith222: 可以先 option+左鍵 點cell 看 cell 是不是 customcell1 01/04 12:04
→ tentenlee: mCell塞進去cell裡面,但是編譯器還是認為cell是UITabl 01/04 12:05
→ tentenlee: eViewCell,所以找不到你子類別另外的屬性,所以出錯 01/04 12:06
→ tentenlee: 在case裡面先把let cell2 = table.....做好後再cell=ce 01/04 12:06
→ tentenlee: ell2應該就沒問題了。 01/04 12:07
→ tentenlee: 但是這樣做 還不如直接把最外面的var cell 這段拿掉 01/04 12:08
→ tentenlee: 在case裡面直接做return cell的動作 除非你還要做啥事 01/04 12:09
→ whitefox0102: 可是在case裡面做return cell,最下面會顯示 01/04 12:13
→ whitefox0102: Missing return in a function expected to return 01/04 12:14
推 tentenlee: 因為你default裡面也要return cell 01/04 12:19
推 tentenlee: 或者是直接在最後一行寫 return UITableViewCell() 01/04 12:22
→ whitefox0102: 成功了!在最後加上return UITableViewCell()就可了 01/04 12:52
→ whitefox0102: 感謝keith222,及tentenlee大大^^ 01/04 12:53