【问题标题】:Setting up an NSTableView programmatically (catching tableView:objectValueForTableColumn:row:)以编程方式设置 NSTableView(捕获 tableView:objectValueForTableColumn:row:)
【发布时间】:2010-01-27 22:22:59
【问题描述】:

我正在尝试以编程方式设置 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 代码,我将尝试添加 cmets 以使非 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

【问题讨论】:

  • 究竟是什么不工作?您的数据源方法没有被调用吗?您是否在表格视图中添加了任何NSTableColumn?如果您以编程方式创建NSTableView,则默认情况下不会有任何表列。您还需要创建和添加它们。
  • 是的,我添加了一个 NsTableColumn,是的,数据源方法没有被调用(我为每个方法添加了一个打印语句只是为了确定)。
  • setDelegate:setDataSource: 应该足够了。 cocoa-lisp 桥接器是否正确处理非对象数据?表视图会在调用之前检查委托对象是否具有委托方法,因此调试它的一种方法可能是覆盖respondsToSelector: 并查看它是否被调用。
  • 滚动视图(及其表格视图)是否在屏幕上?也就是说,它是否在排序的窗口的视图层次结构中?如果是这样,如果您向表格视图发送reloadData 消息会发生什么?这不应该是必要的,但你不妨检查一下。你是如何创建表格视图的?你应该在这个上展示你的代码,即使它是在 Lisp 中的。
  • 是的,我创建了一个带有滚动视图的表格视图。当我加载窗口时,我得到一个只有标题的空表字段的窗口。此外,该窗口有一个垂直滚动条。接下来我将发布一些 lisp 代码。我也尝试重新加载数据,但这些方法仍然没有被调用。

标签: cocoa macos nstableview


【解决方案1】:
    (let ((table-view (#/alloc ns:ns-outline-view)))
              (#/init table-view)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多