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

时间:2010-01-27 22:23:00

标签: cocoa macos nstableview

我正在尝试以编程方式设置NSTableView(我真的需要避免使用IB),我发现的所有示例都解释了如何使用界面构建来完成此操作。我创建了一个实现方法tableView的控制器类:objectValueForTableColumn:row:和numberOfRowsInTableView:但它们永远不会被调用。
在我设置了table-view和scroll-view之后,我调用了setDelegate:和setDataSource:,并使用了实现numberOfRowsInTableView:的控制器类。不幸的是,当我运行代码时,它从不调用numberOfRowsInTableView:或tableView:objectValueForTableColumn:row:。我是否需要调用setDelegate:和setDataSource之外的东西来完成我想要做的事情? 我在clozure-cl(一个包含cocoa-lisp桥的lisp环境)中编写此代码,我相信这里的大多数可可开发人员都不太可能是lisp爱好者,所以发布代码似乎不是很有成效但是如果有人能给我的话任何建议或指向Objective-c代码的链接,在不使用非常棒的IB的情况下完成整个过程。

编辑:

这里有一些我的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:方法的snipet,它是我的控制器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

1 个答案:

答案 0 :(得分:1)

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

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