2017-01-05 1 views
2

간단한 뷰 기반 NSTableView를 만들 수 있지만 식별자에 대해 이해할 수없는 한 가지 점이 있습니다. 당신은 일반적으로 열을 식별자를 제공하고 대리자 메서드를 구현 NSTableView는에서 테이블 셀 뷰의 NSTableView 식별자에 대해 혼동 됨

: 당신이 필요로하는 무엇을 당신이 할 컬럼에 전환 후

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? 

과, 뭔가 같은 :

switch tableColumn!.identifier { 
case "firstColumn": 
    //do something... 
case "secondColumn": 
    //do something else... 
default: 
    return nil 
} 

그러나 추가로 각 테이블에 표 셀보기를 부여 할 수도 있습니다. 그래서 위의 예에서 식별자를 열에 제공하지 않았고 테이블 셀 뷰 자체에 식별자를 부여했다고 가정하십시오..

나는 그때 대리인이 방법이 같은 일을 할 수 있다고 추정 :

if let firstColumnCellView = tableView.make(withIdentifier: "firstColumnCell", owner: self) as? NSTableCellView { 
    view.textField?.stringValue = "Hi! I'm in the first column" 
    return view 
} else if let secondColumnCellView = tableView.make(withIdentifier: "secondColumnCell", owner: self) as? NSTableCellView { 
    view.textField?.stringValue = "Hi! I'm in the second column" 
    return view 
} else { 
    return nil 
} 

이 작동하지만, 첫 번째 if let 문 과거를하지 않으며, 그래서 내 모든 세포 안녕하세요 ' "라고 결코! 다른

뭔가 이해가 안 : 첫 번째 열에 "


추가 정보의 m 보인다 그 표 셀 V 뷰 식별자는 식별자를 열에 우선합니다.

나는 문서 개요에 가서 같은 식별자 뭔가를 할당하는 경우 :

tableColumn: "firstColumn" 
    tableViewCell: "firstColumnCell" 

tableColumn: "secondColumn" 
    tableViewCell: "secondColumnCell" 

을 다음 공급이 열 식별자 및 셀 식별자 둘 다 작동!

switch tableColumn!.identifier { 
    case "firstColumn": 
     if let firstColumnCellView = tableView.make(withIdentifier: "firstColumnCell", owner: self) as? NSTableCellView { 
      view.textField?.stringValue = "Hi! I'm in the first column" 
      return view 
     } else { 
      return nil 
     } 
    case "secondColumn": 
     if let secondColumnCellView = tableView.make(withIdentifier: "secondColumnCell", owner: self) as? NSTableCellView { 
      view.textField?.stringValue = "Hi! I'm in the second column" 
      return view 
     } else { 
      return nil 
     } 
    default: 
     return nil 
    } 

는하지만 충돌 난 스위치 문은 두 번째 열에 대한 셀 식별자를 무시하고 열 식별자를 사용하려고 시도에 이르기까지 떨어지지합니다.

switch tableColumn!.identifier { 
    case "firstColumn": 
     if let firstColumnCellView = tableView.make(withIdentifier: "firstColumnCell", owner: self) as? NSTableCellView { 
      view.textField?.stringValue = "Hi! I'm in the first column" 
      return view 
     } else { 
      return nil 
     } 
    default: 
     break 
    } 


    let cellView = tableView.make(withIdentifier: tableColumn!.identifier, owner: self) as! NSTableCellView 
    cellView.textField?.stringValue = "hello" 
    return cellView 

    //CRASH: Unexpectedly found nil when unwrapping tableColumn!.identifier 
    // The column both exists and has an identifier of "secondColumn", so how could 
    //this be nil? 

그리고 내가 secondColumn 같은 이름으로 secondColumnCell의 이름을 변경 하여이 최우선 동작을 확인할 수 있습니다 보인다

tableColumn: "firstColumn" 
    tableViewCell: "firstColumnCell" 

tableColumn: "secondColumn" <--- Same Name 
    tableViewCell: "secondColumn" <-- Same Name 

를 이제 코드가 예상과 충돌하지 않는 한 실행됩니다. 내가 제대로 코드의 마지막 청크를 읽는다면

답변

0

, 당신은 인스턴스화 (또는 A로부터 사용-전망 노 - 이상 - 화면 풀을 검색 - here 참조) 식별자 firstColumnCell와보기를.

식별자가 유효하면 (뷰를 정의하는 nib가있는 경우) 메소드는 항상 nil이 아닌 뷰를 반환하므로 첫 번째 if let ...이 항상 성공합니다.

따라서 view.textField?.stringValue = "Hi! I'm in the first column"이 실행되어 셀에 메시지가 표시되면 NSTableView에서 사용할보기가 반환되고 메서드가 종료됩니다.

다음 if let ... 문은 절대로 실행할 수 없습니다.

+0

감사합니다. 질문에 대한 추가 정보가 있습니다. 미안해, 아직도 이해 못 하겠어. – MH175