[問題] tableView調用多種Cell

看板MacDev作者 (一個人踢球的小正)時間6年前 (2018/01/04 11:15), 編輯推噓4(4010)
留言14則, 3人參與, 6年前最新討論串1/1
各位大大好 小弟想在一個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

01/04 12:04, 6年前 , 1F
你的cell宣告是UITabeleViewCell雖說你把另外一個custo
01/04 12:04, 1F

01/04 12:04, 6年前 , 2F
可以先 option+左鍵 點cell 看 cell 是不是 customcell1
01/04 12:04, 2F

01/04 12:05, 6年前 , 3F
mCell塞進去cell裡面,但是編譯器還是認為cell是UITabl
01/04 12:05, 3F

01/04 12:06, 6年前 , 4F
eViewCell,所以找不到你子類別另外的屬性,所以出錯
01/04 12:06, 4F

01/04 12:06, 6年前 , 5F
在case裡面先把let cell2 = table.....做好後再cell=ce
01/04 12:06, 5F

01/04 12:07, 6年前 , 6F
ell2應該就沒問題了。
01/04 12:07, 6F

01/04 12:08, 6年前 , 7F
但是這樣做 還不如直接把最外面的var cell 這段拿掉
01/04 12:08, 7F

01/04 12:09, 6年前 , 8F
在case裡面直接做return cell的動作 除非你還要做啥事
01/04 12:09, 8F

01/04 12:13, 6年前 , 9F
可是在case裡面做return cell,最下面會顯示
01/04 12:13, 9F

01/04 12:14, 6年前 , 10F
Missing return in a function expected to return
01/04 12:14, 10F

01/04 12:19, 6年前 , 11F
因為你default裡面也要return cell
01/04 12:19, 11F

01/04 12:22, 6年前 , 12F
或者是直接在最後一行寫 return UITableViewCell()
01/04 12:22, 12F

01/04 12:52, 6年前 , 13F
成功了!在最後加上return UITableViewCell()就可了
01/04 12:52, 13F

01/04 12:53, 6年前 , 14F
感謝keith222,及tentenlee大大^^
01/04 12:53, 14F
文章代碼(AID): #1QJPnT82 (MacDev)