我正在尝试以编程方式设置 NSTableView(我真的需要避免使用 IB),我找到的所有示例都解释了如何使用界面构建来做到这一点。我创建了一个控制器类,它实现了 tableView:objectValueForTableColumn:row: 和 numberOfRowsInTableView: 方法,但它们从未被调用。
在设置表格视图和滚动视图后,我调用了 setDelegate: 和 setDataSource: 以及实现 numberOfRowsInTableView: 的控制器类。不幸的是,当我运行代码时,它从不调用 numberOfRowsInTableView: 或 tableView:objectValueForTableColumn:row:。除了 setDelegate: 和 setDataSource: 之外,我是否需要调用其他东西来完成我想要做的事情?
我正在 clozure-cl(一个包含 cocoa-lisp 桥的 lisp 环境)中编写此代码,我相信这里的大多数可可开发人员不太可能是 lisp 爱好者,所以发布代码似乎没有成果,但如果有人可以给我在不使用 IB 的情况下完成整个过程的任何建议或指向 Objective-c 代码的链接都会很棒。
编辑:
这是我的一些 lisp 代码,我将尝试添加注释,以使非 lisp 可可开发人员更清楚。
(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: 方法的一个片段,它是我的控制器 table-view-controller 的一部分(这个调用是 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