Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios cellForRowAt-从switch语句中创建并返回单元格_Ios_Swift_Uitableview - Fatal编程技术网

Ios cellForRowAt-从switch语句中创建并返回单元格

Ios cellForRowAt-从switch语句中创建并返回单元格,ios,swift,uitableview,Ios,Swift,Uitableview,我认为我遗漏了Swift的一些基本要素,但找不到其他人做我正在尝试的事情的例子: 背景 我有一个带有2个原型单元的UITableView,具有不同的标识、不同的功能(标签、图像等)和不同的类 我希望cellForRowAt函数根据保存表数据的数组中的内容返回不同类型和类别的单元格。该数组由struct实例填充,其中一个特征标识我要表示数据的单元格类型 代码尝试 这不是逐字复制/粘贴,但原理是一样的 func tableView(_ tableView: UITableView, cellForR

我认为我遗漏了Swift的一些基本要素,但找不到其他人做我正在尝试的事情的例子:

背景

我有一个带有2个原型单元的UITableView,具有不同的标识、不同的功能(标签、图像等)和不同的类

我希望cellForRowAt函数根据保存表数据的数组中的内容返回不同类型和类别的单元格。该数组由struct实例填充,其中一个特征标识我要表示数据的单元格类型

代码尝试

这不是逐字复制/粘贴,但原理是一样的

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       switch dataArray[indexPath.item].typeOfData {
       case "Type1":
          let cell = tableView.dequeueReusableCell(withIdentifier: "type1ReuseIdentifier", for: indexPath) as! type1Cell
          //Set up the cell contents
          return cell
       case "Type2":
          let cell = tableView.dequeueReusableCell(withIdentifier: "type2ReuseIdentifier", for: indexPath) as! type2Cell
          //Set up the cell contents
          return cell
       default
          let cell = tableView.dequeueReusableCell(withIdentifier: "separatorIdentifier", for: indexPath) as! separatorCell
          //Set up the cell contents
          return cell
     }
}
问题是这不起作用,Swift希望我在switch语句之外声明并返回单元格,但是我不能这样做,因为单元格类型的声明取决于我要创建的数据类型(因为需要使用!typexCell来访问自定义单元格的组件)

我错过了什么/做错了什么?

就这样做:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let typeSection = CellType(rawValue: indexPath.row)!

    switch typeSection {
    case .CenterTitleCell:
        return getCenterTitleCell(tableView, indexPath)
    case .CenterDescriptionCell:
        return getCenterDescriptionCell(tableView, indexPath)
    case .CenterImageCell:
        return getImageCell(tableView, indexPath)
    case .FooterTitleCell:
        return getFooterViewCell(tableView, indexPath)
    }
}
并使用另一种方法返回单元格类型

func getCenterTitleCell (_ tableView: UITableView, _ indexPath:IndexPath) -> CenterTitleTableViewCell {
    let cell:CenterTitleTableViewCell =  tableView.dequeueReusableCell(withIdentifier: String(describing: CenterTitleTableViewCell.self),
                                                                                   for: indexPath) as! CenterTitleTableViewCell
    cell.selectionStyle = .none
    return cell
}

您是否编写了
default
case?比较。对不起,是的,默认语句也存在,并返回一个分隔符类型的单元格“Swift希望我声明并返回switch语句之外的单元格”-不,我不这么认为。如果switch语句中的所有case都返回一个单元格,那么它应该可以顺利编译。我是个白痴,你说得对Martin,我的一个switch case没有返回!假设只有4行,每行有自己的单元格类型。一般来说,这不是大多数表的设置方式。这是一个简单的示例。在本例中,我有一个提供单元格类型的枚举,因为它是一个静态屏幕。应该根据他想要的代码修改示例。