2016-06-29 3 views
0

NSTableView에 표시 할 행이 없으면 대신 이미지를 표시하고 싶습니다. 내 사용자 지정 NSViewController 클래스에서 다음을 시도했지만 여전히 빈 테이블이 표시됩니다. Assets.xcassets에 "ic_empty_image"가 있습니다.NSTableView : 빈 테이블을 이미지로 바꿉니다.

func numberOfRowsInTableView(tableView: NSTableView) -> Int { 
    if model.numRows == 0 { 
     let image = NSImage(named: "ic_empty_image") 
     let imageView = NSImageView() 
     imageView.image = image 
     self.view.replaceSubview(tableView, with: imageView) 
     self.view.setNeedsDisplayInRect(imageView.visibleRect) 
    } else { 
     return model.numRows 
    } 
} 

답변

1

보기 기반 테이블을 활용하면 빈 데이터 집합이 있음을 알 때 셀보기를 바꿀 수 있습니다. 트릭은 테이블을 배열에 바인딩하고 비어있는 대신 하나의 항목 표시기를 갖는 것입니다.

내가 여기 당신을 위해 샘플 프로젝트를 생성 : https://github.com/emankovski/nstableview-empty-picture

그러나 일반적으로 논리가 실제로 매우 간단하다 : 나는 펜촉에 NSTableCellView 객체를 생성하고 내가 어떤 데이터의 표시를 볼 때 그냥 돌아갑니다. 또한 텍스트가있는 셀보다 이미지 길이가 더 긴 셀을 테이블 뷰의 행 높이로 변경합니다. 나는 그것이 도움이되기를 바랍니다. 스토리 보드에 대한

class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource { 

private static let placeholder = "<Placeholder>" 
dynamic var items = [String]() 

@IBOutlet weak var window: NSWindow! 
@IBOutlet weak var tableView: NSTableView! 
@IBOutlet weak var imageCell: NSTableCellView! 

@IBAction func addRowsPressed(sender: AnyObject) { 
    appendItems() 
} 

@IBAction func clearTablePressed(sender: AnyObject) { 
    removeItems() 

} 

func applicationDidFinishLaunching(aNotification: NSNotification) { 
    appendItems() 

} 

func applicationWillTerminate(aNotification: NSNotification) { 
    // Insert code here to tear down your application 
} 

func appendItems() { 


    items.removeAll() 

    items.append("One") 
    items.append("Two") 
    items.append("Three") 

    tableView.rowHeight = 20 

} 

func removeItems() { 

    items.removeAll() 
    tableView.rowHeight = 50 
    items.append(AppDelegate.placeholder) 


} 

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? { 

    if items[row] == AppDelegate.placeholder { 

     return imageCell 
    } 

    let cellView = tableView.makeViewWithIdentifier("CellView", owner: nil) as! NSTableCellView 

    return cellView 
} 


} 

UPDATE : 스토리 보드에서 컨트롤러를 볼 수 테이블 셀보기를 드래그하는 방법 사진 :

enter image description here

그럼 그냥 콘센트에 객체 드래그를 연결 콘센트를 작성하고

:

@IBOutlet var tableCellView: NSTableCellView! 
+0

유망 해 보입니다. storyboard 아니라 xib 사용하고, cellView 및 imageView간에 테이블 열을 전환 할 수있는 방법이 있나요? 이미지 셀을 내 열로 끌어 왔지만 기존 텍스트 셀을 바꿉니다. – Ravi

+0

오브젝트 라이브러리에서 테이블이 아닌 테이블 뷰 (이미지 또는 텍스트)를 뷰 컨트롤러가 나열된 문서 아웃 라인 영역으로 드래그하십시오. 드래그하여 컨트롤러의 형제가되도록하십시오. –

+0

스토리 보드 지침에 따라 답변을 업데이트합니다. –

-1

테이블보기를 바꾸는 대신 tableView 및 이미지보기의 숨겨진 속성으로 재생하면됩니다. 당신의 기본 클래스가이 일을해야하더라도

private var imageView: UIImageView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     let image = NSImage(named: "ic_empty_image") 
     let imageView = NSImageView() 
     imageView.frame = tableView.frame 
     imageView.image = image 
     self.view.addSubview(imageView) 
     imageView.hidden = true 
    } 

    func numberOfRowsInTableView(tableView: NSTableView) -> Int { 
     if model.numRows == 0 { 
      tableView.hidden = true 
      imageView.hidden = false 
      return 0 // Table view will still be active and therefore we need to give it return value 
     } else { 
      tableView.hidden = false 
      imageView.hidden = true 
      return model.numRows 
     } 
    } 

(테스트하지 않았다) 나는이 길을 가고 권하고 싶지 않다 UIViewController에

경우

작동합니다. 더 나은 방법이 있습니다 (필요한 것이 무엇인지에 따라 다름).

UITableViewController를 사용하는 경우 UITableView의 행 수를 기반으로 배경 이미지를 추가 할 수 있습니다. Storyboards 또는 xibs에 있다면 imageView를 만들고 이미지를 추가 할 수 있습니다. 행 수에 따라 이미지보기를 표시하거나 숨길 필요가 있습니다.

+0

추천하지 않는 이유는 무엇입니까? – Willeke

+0

이 답변은 거의 작동합니다. 그러나 숨겨진보기가 잠시 깜박입니다. – Ravi

관련 문제