NSTableView:浮動(或固定)列 (NSTableView: Floating (or fixed) columns)


問題描述

NSTableView:浮動(或固定)列 (NSTableView: Floating (or fixed) columns)

我想要實現的是這樣的:
DevExpress 網格
固定列的表列

上述鏈接中的表格可以有“固定”列,不隨其他內容滾動。

我知道NSTableView floatsGroupRows 功能,以及 NSScrollViewaddFloatingSubview:forAxis: 方法;但要實現上述一個,這些還不夠:

  • 列不是NSView,首先
  • 表頭和表內容放入2 NSScrollView下單獨的NSClipView(這是NSTableView的默認操作)

所以只要我找不到任何內置的解決方案。我唯一的想法是使用 3 個 NSTableView 彼此相鄰(左側為 +1,右側為 +1);並手動同步其中的垂直滾動。如何同步水平滾動,現在這是一個更難的問題。左右兩側不應該滾動,所以應該“浮動”。對於表格的內容,NSScrollViewaddFloatingSubview:forAxis: 方法應該可以工作 IMO(*); 但列標題是不同的動物。好的,仍然應該有一種方法可以通過修改列的繪製來實現這種浮動行為...

但是,我仍然沒有開始實現上述的,因為我的 NSTableView 已經足夠慢了(NSTableview 基於視圖的滾動性能),我我確信這些加的東西會嚴重減慢它。

有沒有人(更好的)想法如何在 Cocoa 中實現浮動列?非常感謝任何幫助!

編輯

(*): NSScrollViewaddFloatingSubview:forAxis: 對此不起作用。正如我現在所看到的,如果賦予此方法的 NSViewNSTableView 的子視圖,它會得到特殊處理。可能該表將自己的邏輯添加到;現在對我來說,NSTableView 一次只能有 1 個浮動行。


參考解法

方法 1:

I have achieved synchronizing two NSTableViews vertically. Not really clear why you would need three, but anyways. Note that this is all c# code using the Xamarin.Mac lib. These are wrappers over the native Cocoa platform. You will have to convert this to obj c/swift yourself. This shouldn't be difficult.

In awake from nib:

table1.EnclosingScrollView.ContentView.PostsBoundsChangedNotifications = true;
NSNotificationCenter.DefaultCenter.AddObserver (NSView.BoundsChangedNotification, BoundsDidChangeNotification, table1.EnclosingScrollView.ContentView);
table2.EnclosingScrollView.ContentView.PostsBoundsChangedNotifications = true;
NSNotificationCenter.DefaultCenter.AddObserver (NSView.BoundsChangedNotification, BoundsDidChangeNotification, table2.EnclosingScrollView.ContentView);

So every time one of the tables scrolls, the BoundsDidChangeNotification is called, and it takes care of synchronizing the y axis. Note that this works even when the scroll happens due to inertia scrolling or programmatic changes of the bounds (or when zooming in/out or resizing the views etc). Such events might not always trigger events which are specifically intended for "user scroll", and for this reason, this is the better approach. Bellow is the BoundsDidChangeNotification method:

public void BoundsDidChangeNotification (NSNotification o)
    {
        if (o.Object == table1.EnclosingScrollView.ContentView) {
            var bounds = new CGRect (new CGPoint (table2.EnclosingScrollView.ContentView.Bounds.Left, table1.EnclosingScrollView.ContentView.Bounds.Top), table2.EnclosingScrollView.ContentView.Bounds.Size);
            if (bounds == table2.EnclosingScrollView.ContentView.Bounds)
                return;
            table2.ScrollPoint (bounds.Location);
        } else {
            var bounds = new CGRect (new CGPoint(table1.EnclosingScrollView.ContentView.Bounds.Left, table2.EnclosingScrollView.ContentView.Bounds.Top), table1.EnclosingScrollView.ContentView.Bounds.Size);
            if (table1.EnclosingScrollView.ContentView.Bounds == bounds)
                return;
            table1.ScrollPoint (bounds.Location);
        }
    }

Nothing too fancy here... there are some bounds equality checks to avoid an eventual infinite loop (table1 tells table2 to sync and then table2 tels table1 and so on, forever). AFAI remember, if you ScrollPoint to the current scrolled location, bounds did change is not triggered, but since an infinite loop would occur otherwise, I think an extra check is worth it ‑ you can never know what happens in future os x versions.

(by nvirthRadu Simionescu)

參考文件

  1. NSTableView: Floating (or fixed) columns (CC BY‑SA 2.5/3.0/4.0)

#cocoa #nstablecolumn #nstableview #floating






相關問題

為什麼 NSWindow 或 NSView 實例處理它自己的鍵事件,而不是它的委託? (Why does an NSWindow or NSView instance handle its own key events, and not its delegate?)

通過 IKImage 視圖預覽圖像 (Previewg images via IKImage View)

Gaya Jendela Kustom di Kakao (Custom Window Style in Cocoa)

Apakah NSURLConnections termasuk dalam model atau pengontrol saya? (Do NSURLConnections belong in my model or controller?)

Эквівалент ключа NMenuItem не працуе, калі меню схавана (NSMenuItem Key Equivalent not working when menu is hidden)

透明窗口中的圓形 NSView (Rounded NSView in a Transparent Window)

單元格中不可見的 UITextField 已將 userInteractionEnabled 設置為 No (UITextField in cell that is not visible has userInteractionEnabled set to No)

如何為自定義 NSImageRep 子類實現 -draw (How to implement -draw for custom NSImageRep subclass)

從 Objective-C 中的核心數據元素獲取和設置值? (Getting and Setting values from Core Data elements in Objective-C?)

來自 URL 編碼問題的 NSArray (NSArray from URL encoding problem)

停靠欄下的窗口化opengl遊戲中的雙光標 (Double cursor in windowed opengl game under dock)

在特定的 NSTableview 單元格中對角線繪製線條 (Draw Lines Diagonally in a particular NSTableview cell)







留言討論