作者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://webptt.com/cn.aspx?n=bbs/MacDev/M.1515035741.A.202.html
1F:推 tentenlee: 你的cell宣告是UITabeleViewCell虽说你把另外一个custo 01/04 12:04
2F:推 keith222: 可以先 option+左键 点cell 看 cell 是不是 customcell1 01/04 12:04
3F:→ tentenlee: mCell塞进去cell里面,但是编译器还是认为cell是UITabl 01/04 12:05
4F:→ tentenlee: eViewCell,所以找不到你子类别另外的属性,所以出错 01/04 12:06
5F:→ tentenlee: 在case里面先把let cell2 = table.....做好後再cell=ce 01/04 12:06
6F:→ tentenlee: ell2应该就没问题了。 01/04 12:07
7F:→ tentenlee: 但是这样做 还不如直接把最外面的var cell 这段拿掉 01/04 12:08
8F:→ tentenlee: 在case里面直接做return cell的动作 除非你还要做啥事 01/04 12:09
9F:→ whitefox0102: 可是在case里面做return cell,最下面会显示 01/04 12:13
10F:→ whitefox0102: Missing return in a function expected to return 01/04 12:14
11F:推 tentenlee: 因为你default里面也要return cell 01/04 12:19
12F:推 tentenlee: 或者是直接在最後一行写 return UITableViewCell() 01/04 12:22
13F:→ whitefox0102: 成功了!在最後加上return UITableViewCell()就可了 01/04 12:52
14F:→ whitefox0102: 感谢keith222,及tentenlee大大^^ 01/04 12:53