gpt4 book ai didi

cocoa - 以编程方式设置 NSTableView(捕获 tableView :objectValueForTableColumn:row:)

转载 作者:行者123 更新时间:2023-12-03 17:17:52 25 4
gpt4 key购买 nike

我正在尝试以编程方式设置 NSTableView(我确实需要避免使用 IB),并且我找到的所有示例都解释了如何使用界面构建来执行此操作。我创建了一个 Controller 类,它实现了 tableView:objectValueForTableColumn:row: 和 numberOfRowsInTableView: 方法,但它们从未被调用。
设置 TableView 和 ScrollView 后,我使用实现 numberOfRowsInTableView: 的 Controller 类调用 setDelegate: 和 setDataSource: 。不幸的是,当我运行代码时,它永远不会调用 numberOfRowsInTableView: 或 tableView:objectValueForTableColumn:row: 。我是否需要调用 setDelegate: 和 setDataSource: 之外的其他内容来完成我想要做的事情?
我正在 clozure-cl (一个包含 cocoa-lisp 桥的 lisp 环境)中编写这段代码,我相信这里的大多数 cocoa 开发人员不太可能是 lisp 爱好者,所以看起来发布代码不会富有成效,但如果有人能给我任何建议或链接到 Objective-C 代码,在不使用 IB 的情况下完成整个过程,那就太好了。

编辑:

这是我的一些 lisp 代码,我将尝试添加注释,以使非 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: 方法的一个片段,它是我的 Controller 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

最佳答案

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

永远不要将 init 发送到 NSView 类的实例。始终使用 initWithFrame:

关于cocoa - 以编程方式设置 NSTableView(捕获 tableView :objectValueForTableColumn:row:),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2150743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com