2015-06-30 40 views
1

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

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 
     } 


    } 

回答

1

您有默认的分支,没有初始化的单元格变量返回。我会推荐如下更改:

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 
} 
+0

那么解决方案是什么? – chicobermuda

+0

@dprek我更新了我的答案。我在代码中做了一个假设,总是选择一些段,所以我们总是显示CellOne或CellTwo,并且没有任何其他选项。 –

+0

我该如何配置,即CellOne中的标签与CellTwo中的UIImageView? – chicobermuda

相关问题