我正在尝试将多个子类添加到 UITableView 中。问题是它一直给我以下错误:
Type UITableVieCell does not conform to protocol NilLiteralConvertible
CellForRowAtIndexPath
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = self.section1[indexPath.row]
cell.accessoryType = .DisclosureIndicator
return cell
} else if indexPath.section == 1 {
let cell = tableView.dequeueReusableCellWithIdentifier("SwitchViewCell", forIndexPath: indexPath) as SwitchViewCell
cell.cellLabel?.text = self.section2[indexPath.row]
return cell
}
return nil
}
最佳答案
你不能返回零。而不是默认返回空单元格。
var cell :UITableViewCell!
switch (indexPath.row) {
case 0:
cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = self.section1[indexPath.row]
cell.accessoryType = .DisclosureIndicator
break;
case 1:
cell = tableView.dequeueReusableCellWithIdentifier("SwitchViewCell", forIndexPath: indexPath) as SwitchViewCell
(cell as SwitchViewCell).cellLabel?.text = self.section2[indexPath.row]
break;
default:
break;
}
return cell
确保你已经注册了nib
self.tableView.registerNib(UINib(nibName: "SwitchViewCell", bundle: nil), forCellReuseIdentifier: "SwitchViewCell")
关于ios - UITableViewCONtroller 中的快速多个单元格子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26140914/