I am trying to setup an NSTableView programmatically (I really need to avoid using IB) and all the examples I find explain how to do this using the interface building. I have created a controller class that implements the methods tableView:objectValueForTableColumn:row: and numberOfRowsInTableView: but they never get called.
After I set up the table-view and scroll-view I called setDelegate: and setDataSource: with the controller class that implemented numberOfRowsInTableView:. Unfortunately when I run the code it never calls numberOfRowsInTableView: or tableView:objectValueForTableColumn:row:. Do I need to call something besides setDelegate: and setDataSource: to accomplish what I am trying to do?
I am writing this code in clozure-cl (a lisp environment that includes a cocoa-lisp bridge) and I believe most cocoa developers here are unlikely to be lisp enthusiasts so it seems that posting code my not be fruitful but if anyone could give me any suggestions or links to objective-c code that does this whole process without using the the IB that would be great.
EDIT:
Here is a bit of my lisp code I'll try and add comments to make it more clear to non lisp cocoa developers.
(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))))))
Here is a snipet from my numberOfRowsInTableView: method which is part of my controller table-view-controller (this call is a of 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
NSTableColumn
to the table view? If you create anNSTableView
programmatically, there won't be any table columns by default. You'll need to create and add those, too. – AlexsetDelegate:
andsetDataSource:
should be enough. Does the cocoa-lisp bridge handle non-object data correctly? The table view will check if the delegate object has the delegate method before calling it, so one way to debug it might be to overriderespondsToSelector:
and see if it is called. – YujireloadData
message? This shouldn't be necessary, but you might as well check it. How did you create the table view? You should show your code on this one, even though it's in Lisp. – Peter Hosey