我想以编程方式设置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
什么不工作,确切地说?你的数据源方法没有被调用?你有没有添加任何'NSTableColumn'到表格视图?如果以编程方式创建'NSTableView',则默认情况下不会有任何表列。你也需要创建和添加这些。 – Alex 2010-01-27 22:31:07
是的,我添加了一个NsTableColumn,是的数据源方法没有被调用(我只是为了确定每个添加一个打印语句)。 – Mike2012 2010-01-27 22:32:51
'setDelegate:'和'setDataSource:'应该够了。 cocoa-lisp bridge是否正确处理非对象数据?表视图将在调用它之前检查委托对象是否具有委托方法,因此调试它的一种方式可能是重写'respondsToSelector:'并查看它是否被调用。 – Yuji 2010-01-27 22:34:43