UITableViewCOntroller中的swift多个单元格子类 [英] swift multiple cell subclasses in UITableViewCOntroller

查看:258
本文介绍了UITableViewCOntroller中的swift多个单元格子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多个子类添加到UITableView中。问题是它一直给我以下错误:

i'm trying to add multiple subclasses into a UITableView. The problem is that it keep giving me following error:

Type UITableVieCell does not conform to protocol NilLiteralConvertible



CellForRowAtIndexPath



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
}


推荐答案

你不能为cellForRowAtIndexPath返回nil

You are not allowed to return nil for cellForRowAtIndexPath

你可以在最后使用:

let cell:UITableViewCell!
return cell

而不是

 return nil

这应该是(如果你有的话) 2部分)永远不会被执行。

This should be (if you just have 2 sections) never get executed.

编辑:

如果是indexPath,您也可以使用而不是} else .section == 1 {only} else { - 返回一个单元格。我刚刚出现了什么问题。或者使用Switch / Case并默认返回空Cell。

You could also use instead of "} else if indexPath.section == 1 {" only } else { - to return a cell. I just showed up what is the problem. Or use a Switch/Case and returning an empty Cell on default.

这篇关于UITableViewCOntroller中的swift多个单元格子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆