[問題] tableView調用多種Cell

作者: whitefox0102 (一個人踢球的小正)   2018-01-04 11:15:34
各位大大好
小弟想在一個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
作者: tentenlee (天天)   2018-01-04 12:04:00
你的cell宣告是UITabeleViewCell雖說你把另外一個custo
作者: keith222 (Keith)   2018-01-04 12:04:00
可以先 option+左鍵 點cell 看 cell 是不是 customcell1
作者: tentenlee (天天)   2018-01-04 12:05:00
mCell塞進去cell裡面,但是編譯器還是認為cell是UITableViewCell,所以找不到你子類別另外的屬性,所以出錯在case裡面先把let cell2 = table.....做好後再cell=ceell2應該就沒問題了。但是這樣做 還不如直接把最外面的var cell 這段拿掉在case裡面直接做return cell的動作 除非你還要做啥事
作者: whitefox0102 (一個人踢球的小正)   2018-01-04 12:13:00
可是在case裡面做return cell,最下面會顯示Missing return in a function expected to return
作者: tentenlee (天天)   2018-01-04 12:19:00
因為你default裡面也要return cell或者是直接在最後一行寫 return UITableViewCell()
作者: whitefox0102 (一個人踢球的小正)   2018-01-04 12:52:00
成功了!在最後加上return UITableViewCell()就可了感謝keith222,及tentenlee大大^^

Links booklink

Contact Us: admin [ a t ] ucptt.com