使用分段控件返回 cellForRowAtIndexPath 中的两个单元格

Returning two cells in cellForRowAtIndexPath using segmented control

我在两个 UITableViews 之上有一个分段控件,其中一个在另一个之上。当 segmentedControl.selectedSegmentIndex == 1 其中一个 table 视图将隐藏。但是,问题是我无法在我的一个 cellForRowAtIndexPath 函数中配置第二个 table 视图的自定义单元格。我不断收到:Variable 'cell' used before being initialized.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        switch segmentedControl.selectedSegmentIndex {
        case 0:
            var cell = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as! CellOne

            return cell

        case 1:

            var cellTwo = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as! CellTwo

            return cellTwo

        default:
            var cell: UITableViewCell
            return cell
        }


    }

您有默认分支,您 returns 没有初始化单元格变量。我建议进行如下更改:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let result: UITableViewCell

    if (segmentedControl.selectedSegmentIndex == 0) { 
       var cellOne = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as! CellOne
       //Configure here
       result = cellOne
    } else {
       var cellTwo = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as! CellTwo
       //Configure here
       result = cellTwo
    }

    return result
}