Xamarin 如何在Mac中拖放NSTableView中的行
我想到了一点,即现在我使用NSTableView获得了一个数据列表,但我的要求是,能够将该行从一行位置拖放到另一行位置。请给出解决这个问题的建议。提前谢谢Xamarin 如何在Mac中拖放NSTableView中的行,xamarin,xamarin.mac,Xamarin,Xamarin.mac,我想到了一点,即现在我使用NSTableView获得了一个数据列表,但我的要求是,能够将该行从一行位置拖放到另一行位置。请给出解决这个问题的建议。提前谢谢 在NSTableViewDataSource子类中,实现WriteRows、ValidateDrop和AcceptDrop并注册拖放目标为NSTableView接受的对象。在这种情况下,您只能从自己的NSTableView中接受拖放 指定一个名称,该名称将用于此NSTableView上的有效拖动操作: 为您的NSTableView注册拖动类型
在
NSTableViewDataSource
子类中,实现WriteRows
、ValidateDrop
和AcceptDrop
并注册拖放目标为NSTableView
接受的对象。在这种情况下,您只能从自己的NSTableView
中接受拖放
指定一个名称,该名称将用于此NSTableView
上的有效拖动操作:
为您的NSTableView
注册拖动类型:
在NSTableViewDataSource
上实现拖放方法:
嗨,非常感谢你的帮助。我不懂注册表拖放类型。现在我已经在我的视图控制器中注册了拖放类型,当尝试在accept drop方法中写入该拖放类型字符串时,它会显示错误。请帮助我如何解决它。-->我的表接受该方法,但我的问题是您使用了'DragDropType'字符串并插入了一些值,您在其中写入了'typeof(product).FullName',其中什么是产品,另一个问题此registerdrag类型代码在视图控制器中写入,对吗?但“DragDropType”未出现在我的数据源表中。我对此非常陌生,请帮助我。我正在.XIb中创建NSTableView,并且我没有向mytable声明任何数组控制器。因此,据我所知,“product”是表数据源的类名,对吗?我把“DragDropType”字符串搞混了。我已经在我的视图控制器中分配了'typeof'值,我的'writ row'方法存在于tableview数据源类中,那么我如何获得该字符串值。好的,谢谢。我已经试过了,但是当编译时我的应用程序应该关闭了。我曾试图找出问题,但没有找到。我已经发布了我的示例代码,请参考该代码。
// Any name can be registered, I find using the class name
// of the items in the datasource is cleaner than a const string
string DragDropType = typeof(Product).FullName;
ProductTable.RegisterForDraggedTypes(new string[] { DragDropType });
public override bool WriteRows(NSTableView tableView, NSIndexSet rowIndexes, NSPasteboard pboard)
{
var data = NSKeyedArchiver.ArchivedDataWithRootObject(rowIndexes);
pboard.DeclareTypes(new string[] { DragDropType }, this);
pboard.SetDataForType(data, DragDropType);
return true;
}
public override NSDragOperation ValidateDrop(NSTableView tableView, NSDraggingInfo info, nint row, NSTableViewDropOperation dropOperation)
{
tableView.SetDropRowDropOperation(row, dropOperation);
return NSDragOperation.Move;
}
public override bool AcceptDrop(NSTableView tableView, NSDraggingInfo info, nint row, NSTableViewDropOperation dropOperation)
{
var rowData = info.DraggingPasteboard.GetDataForType(DragDropType);
if (rowData == null)
return false;
var dataArray = NSKeyedUnarchiver.UnarchiveObject(rowData) as NSIndexSet;
Console.WriteLine($"{dataArray}");
// Move hack for this example... you need to handle the complete NSIndexSet
tableView.BeginUpdates();
var tmpProduct = Products[(int)dataArray.FirstIndex];
Products.RemoveAt((int)dataArray.FirstIndex);
if (Products.Count == row - 1)
Products.Insert((int)row - 1 , tmpProduct);
else
Products.Insert((int)row, tmpProduct);
tableView.ReloadData();
tableView.EndUpdates();
return true;
}