2010-01-27 53 views
1

我想以編程方式設置NSTableView(我真的需要避免使用IB),並且我找到的所有示例都解釋瞭如何使用接口構建來完成此操作。我創建了一個控制器類,它實現了tableView:objectValueForTableColumn:row:和numberOfRowsInTableView方法:但他們永遠不會被調用。
我設置了table-view和scroll-view之後,我調用setDelegate:和setDataSource:與實現numberOfRowsInTableView:的控制器類相關聯。不幸的是,當我運行代碼時,它從不會調用numberOfRowsInTableView:或tableView:objectValueForTableColumn:row :.我是否需要調用除setDelegate之外的其他東西:setDataSource:完成我正在嘗試做的事情?
我在clozure-cl(包含一個可可lisp橋的lisp環境)編寫這段代碼,我相信這裏的大多數可可開發人員不太可能是lisp愛好者,所以似乎發佈代碼我不是富有成效的,但如果任何人都可以給我任何建議或鏈接到目標C代碼,完成整個過程,而不使用IB將是偉大的。以編程方式設置NSTableView(捕捉tableView:objectValueForTableColumn:行:)

編輯:

這裏有一點我Lisp代碼我會嘗試並添加註釋,使之更加明確非口齒不清Cocoa開發。

(defmethod DISPLAY-TABLE-VIEW ((Self table-view)) 
(with-simple-restart (cancel-pop "Stop trying to pop up ~s" Self) 
(in-main-thread() 
    (ccl::with-autorelease-pool 
     ;let statements are just declarations of local variables in lisp 
     ;this one just creates the window I want to put the table-view inside 
     (let ((Window (make-instance 'popup-window 
         :lui-window Self 
         :with-content-rect (ns:make-ns-rect (x self) (y self) (width Self) 500) 
         :style-mask 3 
         :backing #$NSBackingStoreBuffered 
         :defer t))) 
     (setf (native-window Self) Window) ;; need to have this reference for the delegate to be in place 
     (setf (native-view Self) (make-instance 'popup-window-view :lui-window Self))    
     ;; setup delegate 
     (setf (delegate Window) (make-instance 'popup-delegate :lui-window Self)) 
     (#/setDelegate: Window (delegate Window)) 
     ; (#/setStyleMask: Window 1) 
     ;here I create first a table-view then a scroll-view the an NSView and 
     ;finally a column 
     (let ((table-view (#/alloc ns:ns-outline-view))) 
      (let ((scroll-view (#/alloc ns:ns-scroll-view))) 
      (let ((content-view (#/alloc ns:ns-view))) 
       (let ((column (#/alloc ns:ns-table-column)))    
       (setf content-view (#/contentView Window)) 
       (ns:with-ns-rect (Frame 0 0 100 100) 
        ;here we initialize the table-view the scroll-view and column then 
        ;add the column 
        (#/init table-view) 
        (#/initWithFrame: scroll-view (#/frame (#/contentView Window))) 
        (#/initWithIdentifier: column (native-string "0")) 
        (#/addTableColumn: table-view column) 
        ;make an instance of my controller class which is an nsObject 
        ;I also tried to make it an NSWindowController but that didn't help 
        (let ((package-view (make-instance 'table-view-controller))) 
        ;set the data source and the delegate 

        (#/setDataSource: table-view package-view) 
        (#/setDelegate: table-view package-view))) 
       (#/setHasVerticalScroller: scroll-view #$YES) 
       (#/setDocumentView: scroll-view table-view) 
       (#/setAutoresizesSubviews: (#/contentView Window) #$YES) 

       (#/addSubview: (#/contentView Window) scroll-view)))) 
      (#/reloadData: table-view)) 
     (#/setHasShadow: Window #$YES) 
     ;display the window 
     (#/makeKeyAndOrderFront: Window Window)))))) 

這裏是從我numberOfRowsInTableView一個snipet:方法這是我的控制器表 - 視圖 - 控制器的一部分(此呼叫是NSObject的的)。

(objc:defmethod (#/numberOfRowsInTableView: #>NSInteger) 
    ((self table-view-controller) (tab :id)) 
    (print "hello") 
    ;;... then do other things but just seeing the print statement would be a great step 
+0

什麼不工作,確切地說?你的數據源方法沒有被調用?你有沒有添加任何'NSTableColumn'到表格視圖?如果以編程方式創建'NSTableView',則默認情況下不會有任何表列。你也需要創建和添加這些。 – Alex 2010-01-27 22:31:07

+0

是的,我添加了一個NsTableColumn,是的數據源方法沒有被調用(我只是爲了確定每個添加一個打印語句)。 – Mike2012 2010-01-27 22:32:51

+1

'setDelegate:'和'setDataSource:'應該夠了。 cocoa-lisp bridge是否正確處理非對象數據?表視圖將在調用它之前檢查委託對象是否具有委託方法,因此調試它的一種方式可能是重寫'respondsToSelector:'並查看它是否被調用。 – Yuji 2010-01-27 22:34:43

回答

1
(let ((table-view (#/alloc ns:ns-outline-view))) 
       (#/init table-view) 

永遠不要發送init到的NSView類的一個實例。始終使用initWithFrame:

相關問題