Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using CoreGraphics;
using Foundation;
using UIKit;
namespace CustomDragExample
{
public class CustomView : UIView
{
protected CustomView (IntPtr handle) : base (handle)
{
}
public CustomView () : base ()
{
}
public override void MovedToWindow ()
{
Layer.BackgroundColor = new CGColor (1, 0, 0);
}
}
public class TableHandler : NSObject, IUITableViewDelegate, IUITableViewDataSource, IUITableViewDropDelegate
{
List<string> Data = new List<string> () { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight" };
public const string MyCellIdentifier = "ViewController.CellIdentifier";
[Export ("tableView:cellForRowAtIndexPath:")]
public UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (MyCellIdentifier, indexPath);
cell.TextLabel.Text = Data[(int)indexPath.Item];
return cell;
}
[Export ("tableView:numberOfRowsInSection:")]
public nint RowsInSection (UITableView tableView, nint section) => Data.Count;
[Export ("tableView:canEditRowAtIndexPath:")]
public bool CanEditRow (UITableView tableView, NSIndexPath indexPath) => true;
[Export ("tableView:performDropWithCoordinator:")]
public void PerformDrop (UITableView tableView, IUITableViewDropCoordinator coordinator)
{
// BUG
Console.WriteLine ("PerformDrop");
var destPath = coordinator.DestinationIndexPath;
if (coordinator.Session.CanLoadObjects (typeof (NSString))) {
coordinator.Session.LoadObjects (typeof (NSString), (items => {
Console.WriteLine ("LoadObjects");
// I would expect this to work
var strings = items as NSString[];
Console.WriteLine (strings == null);
// But the underlying item is a string
var v = ObjCRuntime.Runtime.GetNSObject<NSString> (items[0].Handle);
Console.WriteLine (v);
}));
}
}
}
public class DragHandler : NSObject, IUIDragInteractionDelegate, IUIDropInteractionDelegate
{
[Export ("dragInteraction:itemsForBeginningSession:")]
public UIDragItem[] GetItemsForBeginningSession (UIDragInteraction interaction, IUIDragSession session)
{
NSString s = (NSString)"asdf";
NSItemProvider p = new NSItemProvider (s);
return new UIDragItem[] { new UIDragItem (p) };
}
[Export ("tableView:canHandleDropSession:")]
public bool CanHandleDropSession (UITableView tableView, IUIDropSession session) => true;
[Export ("tableView:dropSessionDidUpdate:withDestinationIndexPath:")]
public UITableViewDropProposal DropSessionDidUpdate (UITableView tableView, IUIDropSession session, NSIndexPath destinationIndexPath)
{
if (tableView.HasActiveDrag) {
if (session.Items.Length > 1)
return new UITableViewDropProposal (UIDropOperation.Cancel);
return new UITableViewDropProposal (UIDropOperation.Move, UITableViewDropIntent.InsertAtDestinationIndexPath);
}
return new UITableViewDropProposal (UIDropOperation.Copy, UITableViewDropIntent.InsertAtDestinationIndexPath);
}
}
public partial class ViewController : UIViewController
{
TableHandler TableBehavior = new TableHandler ();
DragHandler DragBehavior = new DragHandler ();
protected ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
UIStackView stack = new UIStackView (View.Bounds) {
Axis = UILayoutConstraintAxis.Horizontal,
Spacing = 10
};
View.AddSubview (stack);
CustomView leftView = new CustomView {
UserInteractionEnabled = true
};
var drag = new UIDragInteraction (DragBehavior) {
Enabled = true
};
leftView.AddInteraction (drag);
UITableView rightView = new UITableView () {
Delegate = TableBehavior,
DataSource = TableBehavior,
DropDelegate = TableBehavior
};
rightView.RegisterClassForCellReuse (typeof (UITableViewCell), TableHandler.MyCellIdentifier);
stack.AddArrangedSubview (leftView);
stack.AddArrangedSubview (rightView);
SetupConstraints (stack, leftView, rightView);
}
void SetupConstraints (UIStackView stack, UIView leftView, UIView rightView)
{
var margins = View.LayoutMarginsGuide;
stack.TranslatesAutoresizingMaskIntoConstraints = false;
stack.LeadingAnchor.ConstraintEqualTo (margins.LeadingAnchor).Active = true;
stack.TrailingAnchor.ConstraintEqualTo (margins.TrailingAnchor).Active = true;
stack.TopAnchor.ConstraintEqualTo (margins.TopAnchor, 15).Active = true;
stack.BottomAnchor.ConstraintEqualTo (margins.BottomAnchor, -15).Active = true;
leftView.WidthAnchor.ConstraintEqualTo (margins.WidthAnchor, (nfloat).33).Active = true;
rightView.WidthAnchor.ConstraintEqualTo (margins.WidthAnchor, (nfloat).66).Active = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.