Drag and Drop files from Finder to a NSTableView

FaisalGhaffarFaisalGhaffar USMember ✭✭

Hi,
I am building a Mac App for File uploading and need to provide a functionality which allows dragging file(s) from Finder/Desktop etc onto the app window which will subsequently list those files in an NSTableView.
I don't have objective c or swift background but have tried getting things out of following posts:
stackoverflow.com/questions/23008530/drag-and-drop-from-the-finder-to-a-nstableview-weirdness
stackoverflow.com/questions/10308008/nstableview-and-drag-and-drop-from-finder?rq=1

In AwakeFromNib()

DocumentsTable.RegisterForDraggedTypes(new string[] { NSPasteboard.NSFilenamesType, NSPasteboard.NSPasteboardTypePNG});

AND

public class DocumentTableDataSource : NSTableViewDataSource
    {
        #region Public Variables
        public List<AttachmentModel> Documents = new List<AttachmentModel>();
        #endregion

        #region Constructors
        public DocumentTableDataSource()
        {
        }
        #endregion

        #region Override Methods
        public override nint GetRowCount(NSTableView tableView)
        {
            return Documents.Count;
        }

        public override bool AcceptDrop(NSTableView tableView, NSDraggingInfo info, nint row, NSTableViewDropOperation dropOperation)
        {
            Console.WriteLine("AcceptDrop");
            return true;
        }
        public override NSDragOperation ValidateDrop(NSTableView tableView, NSDraggingInfo info, nint row, NSTableViewDropOperation dropOperation)
        {
            Console.WriteLine("ValidateDrop");
            return base.ValidateDrop(tableView, info, row, dropOperation);
        }
        public override void DraggingSessionWillBegin(NSTableView tableView, NSDraggingSession draggingSession, CGPoint willBeginAtScreenPoint, NSIndexSet rowIndexes)
        {
            Console.WriteLine("Dragging.......");
        }
        public override void UpdateDraggingItems(NSTableView tableView, NSDraggingInfo draggingInfo)
        {
            Console.WriteLine("UpdateDraggingItems");
        }
        public override bool WriteRows(NSTableView tableView, NSIndexSet rowIndexes, NSPasteboard pboard)
        {
            Console.WriteLine("WriteRows");
            return true;
        }
        #endregion
    }

None of the above methods (AcceptDrop,ValidateDrop,DraggingSessionWillBegin,UpdateDraggingItems,WriteRows) is hit which I drag a file onto the tableView.

What am I missing?

Best Answer

  • FaisalGhaffarFaisalGhaffar US ✭✭
    edited May 2018 Accepted Answer

    Answering my own question.
    I had to subclass NSTableView as well as NSTableViewDataSource.
    In xcode select the table view and in identity inspector provide a custom class e.g. DocumentsTableViewDropper. Switch to visual studio for synchronization and a new class will be created. In this newly created class you can hook the events like

    [Export("draggingEntered:")]
            public override NSDragOperation DraggingEntered(NSDraggingInfo sender)
            {
                return NSDragOperation.Copy;
                //throw new System.NotImplementedException();
            }
    

Answers

  • FaisalGhaffarFaisalGhaffar USMember ✭✭
    edited May 2018 Accepted Answer

    Answering my own question.
    I had to subclass NSTableView as well as NSTableViewDataSource.
    In xcode select the table view and in identity inspector provide a custom class e.g. DocumentsTableViewDropper. Switch to visual studio for synchronization and a new class will be created. In this newly created class you can hook the events like

    [Export("draggingEntered:")]
            public override NSDragOperation DraggingEntered(NSDraggingInfo sender)
            {
                return NSDragOperation.Copy;
                //throw new System.NotImplementedException();
            }
    
Sign In or Register to comment.