2015-06-30 38 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

相關問題