2014-10-02 3 views
1

tableView에서 다른 셀을 반환하려고합니다. 일반적으로이 경우 다른 셀을 반환하고 아래쪽에 nil을 반환하지만이 경우에는 오류가 발생합니다. 나는 빈 셀을 반환하려고했지만, 또한 나에게 오류를 준다. 내가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 

당신이 할 수있는 UITableViewCell 인스턴스를 작성하는 것보다 더 나은 코드를 작성하십시오.이 인스턴스는 Xcode 경고를 자동으로 들으려고 호출되지 않습니다.

그래서 해결책은 무엇입니까?

중요한 것은 에 대한 가능한 마지막 값이 다른 문 (안 else if에서) else에 설정되어있는 경우 있는지 확인하는 것입니다. 같은 방법으로 중요한 것은 스위치 문에 대한 마지막 값이 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

관련 문제