問題描述
NSTableView:浮動(或固定)列 (NSTableView: Floating (or fixed) columns)
我想要實現的是這樣的:
DevExpress 網格
固定列的表列
上述鏈接中的表格可以有“固定”列,不隨其他內容滾動。
我知道NSTableView
的 floatsGroupRows
功能,以及 NSScrollView
的 addFloatingSubview:forAxis:
方法;但要實現上述一個,這些還不夠:
- 列不是
NSView
,首先 - 表頭和表內容放入2
NSScrollView
下單獨的NSClipView
(這是NSTableView
的默認操作)
所以只要我找不到任何內置的解決方案。我唯一的想法是使用 3 個 NSTableView
彼此相鄰(左側為 +1,右側為 +1);並手動同步其中的垂直滾動。如何同步水平滾動,現在這是一個更難的問題。左右兩側不應該滾動,所以應該“浮動”。對於表格的內容,(*); 但列標題是不同的動物。好的,仍然應該有一種方法可以通過修改列的繪製來實現這種浮動行為...NSScrollView
的 addFloatingSubview:forAxis:
方法應該可以工作 IMO
但是,我仍然沒有開始實現上述的,因為我的 NSTableView
已經足夠慢了(NSTableview 基於視圖的滾動性能),我我確信這些加的東西會嚴重減慢它。
有沒有人(更好的)想法如何在 Cocoa 中實現浮動列?非常感謝任何幫助!
編輯
(*): NSScrollView
的 addFloatingSubview:forAxis:
對此不起作用。正如我現在所看到的,如果賦予此方法的 NSView
是 NSTableView
的子視圖,它會得到特殊處理。可能該表將自己的邏輯添加到;現在對我來說,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 nvirth、Radu Simionescu)