2014-10-02 44 views
1

我想返回tableView中的不同單元格。通常在這種情況下,我會返回不同的單元格,然後在底部返回零,但在這種情況下,它會給我和錯誤。我試過也返回一個空單元格,但也給我和錯誤。不能返回單元格cellForRowAtIndexPath

我已經試過

return nil 

var cell: UITableViewCell! 
    return cell 

但兩者返回的錯誤。我怎樣才能解決這個問題?

cellForRowAtIndex

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




    if indexPath.row == 0 { 


     let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as UITableViewCell 
     var imageFile = cell.viewWithTag(100) as PFImageView 
     imageFile.image = itemFile 


     cell.selectionStyle = UITableViewCellSelectionStyle.None 
     return cell 

    } else if indexPath.row == 1 { 
     let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("segmentCell", forIndexPath: indexPath) as UITableViewCell 

     var titleLabel = cell.viewWithTag(101) as UILabel? 
     titleLabel?.text = itemTitle 

     let segmentControl = cell.viewWithTag(102) as UISegmentedControl 
     segmentControl.selectedSegmentIndex = segment 
     segmentControl.setTitle("Beskrivelse", forSegmentAtIndex: 0) 
     segmentControl.setTitle("Sælger", forSegmentAtIndex: 1) 
     segmentControl.setTitle("Lokation", forSegmentAtIndex: 2) 
     segmentControl.tintColor = UIColor(rgba: "#619e00") 
     var font = UIFont(name: "Lato-Regular", size: 11) 
     var attributes:NSDictionary = NSDictionary(object: font , forKey: NSFontAttributeName) 
     segmentControl.setTitleTextAttributes(attributes, forState: UIControlState.Normal) 
     segmentControl.addTarget(self, action: "segmentAction:", forControlEvents: .ValueChanged) 


     cell.selectionStyle = UITableViewCellSelectionStyle.None 

     return cell 

    } else if indexPath.row == 2 { 
     switch segment { 
     case 0: 
      let cell = tableView.dequeueReusableCellWithIdentifier("CellZero", forIndexPath: indexPath) as DescViewCell 
      return cell 
     case 1: 
      let cell = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as SellerViewCell 
      return cell 
     case 2: 
      let cell = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as LocationViewCell 
      return cell 
     default: 
      break 

     } 
    } 


    var cell: UITableViewCell! 
    return cell 
} 

回答

1

你不能從cellForRowAtIndexPath返回nil。你總是應該返回一個有效的單元格。

var cell: UITableViewCell! 

這行代碼不會產生任何細胞,它只是零含量的UITableViewCell變量,你必須一個值分配給它:

var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
return cell 
0

你只聲明最後var cell: UITableViewCell! - 你不是初始化它。你需要做類似

var cell = UITableViewCell(style: someStyle, reuseIdentifier: someID) 
return cell 
1

您需要申報細胞您的if...then邏輯,然後返回。您不必如果使用var初始化單元:

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

    var cell: UITableViewCell! 

    if indexPath.row == 0 { 
     cell = tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as UITableViewCell 
     // ... 
    } else if indexPath.row == 1 { 
     cell = tableView.dequeueReusableCellWithIdentifier("segmentCell", forIndexPath: indexPath) as UITableViewCell 
     // ... 
    } else if indexPath.row == 2 { 
     // ... 
    } 

    return cell 
} 

(只要確保你趕上所有的情況下 - 如果沒有初始化,你會得到一個運行時錯誤的恢復單元。)

5

由於在previous answer for a similar question中指示,-tableView:cellForRowAtIndexPath:必須返回UITableViewCell。所以你不能返回nil。不過,我也建議避免在-tableView:cellForRowAtIndexPath:結束返回下面的代碼時,你是否使用其他開關語句裏面:

//bad code design 
var cell: UITableViewCell! 
return cell 

或:

//bad code design 
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
return cell 

你可以做更好的代碼,而不是創建一個從未被調用過的實例,以便靜默Xcode警告!

那麼解決方案是什麼?

最關鍵的事情是可以肯定的,對於一個你最後可能的值,如果其他語句在else設置(不else if)。同樣,關鍵是要確保開關聲明的最後可能值設置在default:(不在case XXX:中)。

因此,你的代碼應該是這樣的:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if indexPath.row == 0 { 
     let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("CellZero", forIndexPath: indexPath) as UITableViewCell 
     /* ... */ 
     return cell 
    } else if indexPath.row == 1 { 
     let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as CellOne 
     /* ... */ 
     return cell 
    } else { //set your last indexPath.row case in "else", not in "else if indexPath.row == 2"!!! 
     switch segment { 
     case 0: 
      let cell = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as CellTwo 
      /* ... */ 
      return cell 
     case 1: 
      let cell = tableView.dequeueReusableCellWithIdentifier("CellThree", forIndexPath: indexPath) as CellThree 
      /* ... */ 
      return cell 
     default: //set your last segment case in "default:", not in "case 2:"!!! 
      let cell = tableView.dequeueReusableCellWithIdentifier("CellFour", forIndexPath: indexPath) as CellFour 
      /* ... */ 
      return cell 
     } 
    } 
    //No need for a fictive "return cell" with this code!!! 
} 

如果segment是不可選的,這要歸功於元組,您可以將以前的代碼,甚至減少到這一點:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    switch (indexPath.row, segment) { 
    case (0, _): 
     let cell = tableView.dequeueReusableCellWithIdentifier("CellZero", forIndexPath: indexPath) as UITableViewCell 
     /* ... */ 
     return cell 
    case (1, _): 
     let cell = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as CellOne 
     /* ... */ 
     return cell 
    case (2, 0): 
     let cell = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as CellTwo 
     /* ... */ 
     return cell 
    case (2, 1): 
     let cell = tableView.dequeueReusableCellWithIdentifier("CellThree", forIndexPath: indexPath) as CellThree 
     /* ... */ 
     return cell 
    default: //case (2, 2) 
     let cell = tableView.dequeueReusableCellWithIdentifier("CellFour", forIndexPath: indexPath) as CellFour 
     /* ... */ 
     return cell 
    } 
} 
+0

的'default:// case(2,2)'有點不雅。也許使用'fallthrough'?但我甚至不喜歡那樣。 – pkamb 2015-09-20 01:42:07

相關問題