私はついにそれを機能させることができました。試してみたい人のために、いくつかのコードを示します。基本的に、NSTableViewDelegate
必要な関数の s を記述する必要があります。この実装は、コントロールなどもキャッシュしません。Cocoa APIのドキュメントでは、ID を使用してコントロールなどを再利用することが記載されていますが、MonoMac では ID フィールドは取得専用です。
また、データソース自体に関数を実装することNSTableViewDelegate
になりましたが、これはまったくコーシャではないと確信していますが、ベストプラクティスが何であるかはわかりません。
データ ソース クラスは次のとおりです。
class MyTableViewDataSource : NSTableViewDataSource
{
private NSObject[] _data;
// I'm coming from an NSCollectionView, so my data is already in this format
public MyTableViewDataSource(NSObject[] data)
{
_data = data;
}
public override int GetRowCount(NSTableView tableView)
{
return _data.Length;
}
#region NSTableViewDelegate Methods
public NSView GetViewForItem(NSTableView tableView, NSTableColumn tableColumn, int row)
{
// MyViewClass extends NSView
MyViewClass result = tableView.MakeView("MyView", this) as MyViewClass;
if (result == null)
{
result = new MyViewClass(_data[row]);
result.Frame = new RectangleF(0, 0, tableView.Frame.Width, 100); // height doesn't matter since that is managed by GetRowHeight
result.NeedsDisplay = true;
// result.Identifier = "MyView"; // this line doesn't work because Identifier only has a getter
}
return result;
}
public float GetRowHeight(NSTableView tableView, int row)
{
float height = FigureOutHeightFromData(_data[row]); // run whatever algorithm you need to get the row's height
return height;
}
#endregion
}
プログラムでテーブルを作成するスニペットは次のとおりです。
var tableView = new NSTableView();
var dataSource = new MyTableViewDataSource();
tableView.DataSource = dataSource;
tableView.HeaderView = null; // get rid of header row
tableView.GetViewForItem = dataSource.GetViewForItem;
tableView.GetRowHeight = dataSource.GetRowHeight;
AddSubView(tableView);
したがって、行の高さを手動で計算する必要があるため、完全ではありませんがStackPanel
、何もないよりはましです。