ios - UITableViewCONtroller 中的快速多个单元格子类

标签 ios objective-c iphone uitableview swift

我正在尝试将多个子类添加到 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/

相关文章:

ios - 声明选择器 presentviewcontroller

iphone - 单击 UISearchbar (searchDisplayController) 时应用程序崩溃

iphone - 代码 : How to change all fonts at once in an app?

ios - 如何将数组传递给 SOAP API?

objective-c - NSManagedObjectIDs 数组,一次获取对象

iphone - 使用 CCSpriteFrameCache 为动画 Sprite 添加标签

ios - 子类化 UIButton

ios - 当我尝试创建从 UIWebview 到头文件的导出时,Xcode 崩溃

ios - 如何在兼容 iphone 5 和 iphone 4 的 nib 中应用自动布局?

iphone - UIView ivar 与标记本地 UIView var