各位大大好
小弟想在一个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
你的cell宣告是UITabeleViewCell虽说你把另外一个custo
作者:
keith222 (Keith)
2018-01-04 12:04:00可以先 option+左键 点cell 看 cell 是不是 customcell1
mCell塞进去cell里面,但是编译器还是认为cell是UITableViewCell,所以找不到你子类别另外的属性,所以出错在case里面先把let cell2 = table.....做好后再cell=ceell2应该就没问题了。但是这样做 还不如直接把最外面的var cell 这段拿掉在case里面直接做return cell的动作 除非你还要做啥事
可是在case里面做return cell,最下面会显示Missing return in a function expected to return
因为你default里面也要return cell或者是直接在最后一行写 return UITableViewCell()
成功了!在最后加上return UITableViewCell()就可了感谢keith222,及tentenlee大大^^