void MoveItems(NSIndexPath destinationIndexPath, IUITableViewDropCoordinator coordinator) { var destinationAlbum = Album(destinationIndexPath); foreach (var item in coordinator.Items) { var dragItem = item.DragItem; var photo = dragItem.LocalObject as Photo; if (photo is null) { return; } UpdatePhotoLibrary((photoLibrary) => { photoLibrary.MovePhoto(photo, destinationAlbum); }); var cell = TableView.CellAt(destinationIndexPath) as AlbumTableViewCell; if (cell != null) { var rect = cell.RectForAlbumThumbnail; if (!rect.HasValue) { rect = new CGRect(cell.ContentView.Center, CGSize.Empty); } coordinator.DropItemIntoRow(dragItem, destinationIndexPath, rect.Value); } UpdateVisibleAlbumsAndPhotos(); } }
/// <summary> /// Performs the drop. /// </summary> /// <param name="tableView">Table view.</param> /// <param name="coordinator">Coordinator.</param> public void PerformDrop(UITableView tableView, IUITableViewDropCoordinator coordinator) { var destinationIndexPath = coordinator.DestinationIndexPath; if (destinationIndexPath == null) { return; } coordinator.Session.LoadObjects <NSString>(items => { var path = items[0].ToString().Split(new char[] { ',' }, StringSplitOptions.None).Select(x => int.Parse(x)).ToList(); var secIdx = path[0]; var rowIdx = path[1]; if (secIdx != destinationIndexPath.Section) { return; } var section = Element.Model.GetSection(secIdx); if (section.ItemsSource == null) { var tmp = section[rowIdx]; section.RemoveAt(rowIdx); section.Insert(destinationIndexPath.Row, tmp); } else { var tmp = section.ItemsSource[rowIdx]; section.ItemsSource.RemoveAt(rowIdx); section.ItemsSource.Insert(destinationIndexPath.Row, tmp); } }); }
void LoadAndInsertItems(NSIndexPath destinationIndexPath, IUITableViewDropCoordinator coordinator) { var destinationAlbum = Album(destinationIndexPath); foreach (var item in coordinator.Items) { var dragItem = item.DragItem; if (dragItem.ItemProvider.CanLoadObject(typeof(UIImage))) { dragItem.ItemProvider.LoadObject <UIImage>((droppedImage, err) => { var image = droppedImage as UIImage; if (image != null) { var photo = new Photo(image); UpdatePhotoLibrary((photoLibrary) => { photoLibrary.Add(photo, destinationAlbum); }); UpdateVisibleAlbumsAndPhotos(); } }); var cell = TableView.CellAt(destinationIndexPath) as AlbumTableViewCell; if (cell != null) { var rect = cell.RectForAlbumThumbnail; if (!rect.HasValue) { rect = new CGRect(cell.ContentView.Center, CGSize.Empty); //?? } coordinator.DropItemIntoRow(dragItem, destinationIndexPath, rect.Value); } } } }
/// <summary> /// Performs the drop. /// </summary> /// <param name="tableView">Table view.</param> /// <param name="coordinator">Coordinator.</param> public void PerformDrop(UITableView tableView, IUITableViewDropCoordinator coordinator) { var destinationIndexPath = coordinator.DestinationIndexPath; if (destinationIndexPath == null) { return; } coordinator.Session.LoadObjects <NSString>(items => { var path = items[0].ToString().Split(new char[] { ',' }, StringSplitOptions.None).Select(x => int.Parse(x)).ToList(); var secIdx = path[0]; var rowIdx = path[1]; //if(secIdx != destinationIndexPath.Section){ // return; //} var section = Element.Model.GetSection(secIdx); var destSection = Element.Model.GetSection(destinationIndexPath.Section); if (!destSection.UseDragSort) { return; } if (section.ItemsSource == null) { //section.MoveCellWithoutNotify(rowIdx, destinationIndexPath.Row); Control.BeginUpdates(); var cell = section.DeleteCellWithoutNotify(rowIdx); Control.DeleteRows(GetPaths(secIdx, rowIdx, 1), UITableViewRowAnimation.Fade); Control.EndUpdates(); Control.BeginUpdates(); destSection.InsertCellWithoutNotify(cell, destinationIndexPath.Row); Control.InsertRows(GetPaths(destinationIndexPath.Section, destinationIndexPath.Row, 1), UITableViewRowAnimation.None); Control.EndUpdates(); } else { //section.MoveSourceItemWithoutNotify(rowIdx, destinationIndexPath.Row); Control.BeginUpdates(); var deletedSet = section.DeleteSourceItemWithoutNotify(rowIdx); Control.DeleteRows(GetPaths(secIdx, rowIdx, 1), UITableViewRowAnimation.Fade); // Important! An afterimage is someitmes displayed when using NONE. Control.EndUpdates(); Control.BeginUpdates(); destSection.InsertSourceItemWithoutNotify(deletedSet.Cell, deletedSet.Item, destinationIndexPath.Row); Control.InsertRows(GetPaths(destinationIndexPath.Section, destinationIndexPath.Row, 1), UITableViewRowAnimation.None); Control.EndUpdates(); } }); }
/// <summary> /// his delegate method is the only opportunity for accessing and loading /// the data representations offered in the drag item.The drop coordinator /// supports accessing the dropped items, updating the table view, and specifying /// optional animations.Local drags with one item go through the existing /// `tableView(_:moveRowAt:to:)` method on the data source. /// </summary> public void PerformDrop(UITableView tableView, IUITableViewDropCoordinator coordinator) { NSIndexPath indexPath, destinationIndexPath; if (coordinator.DestinationIndexPath != null) { indexPath = coordinator.DestinationIndexPath; destinationIndexPath = indexPath; } else { // Get last index path of table view var section = tableView.NumberOfSections() - 1; var row = tableView.NumberOfRowsInSection(section); destinationIndexPath = NSIndexPath.FromRowSection(row, section); } coordinator.Session.LoadObjects <NSString>((items) => { if (items.Length <= 0) { return; } // Consume drag items List <string> stringItems = new List <string>(); foreach (var i in items) { var q = NSString.FromHandle(i.Handle); stringItems.Add(q.ToString()); } var indexPaths = new List <NSIndexPath>(); tableView.BeginUpdates(); for (var j = 0; j < stringItems.Count; j++) { var indexPath1 = NSIndexPath.FromRowSection(destinationIndexPath.Row + j, 0); // update backing data InsertTodo(new TodoItem { Name = stringItems[j] }, destinationIndexPath.Row); indexPaths.Add(indexPath1); } todoItems = AppDelegate.Current.TodoMgr.GetOrderedTodos().ToList(); // animate table view to match if (indexPaths.Count > 0 && indexPaths.Count == stringItems.Count) { tableView.InsertRows(indexPaths.ToArray(), UITableViewRowAnimation.Automatic); } tableView.EndUpdates(); }); }
//[Export("tableView:performDrop:")] public void PerformDrop(UITableView tableView, IUITableViewDropCoordinator coordinator) { var sourceCollection = ((IList)DragAndDropListViewRenderer._sourceListView.ItemsSource); ListView targetListView; (tableView.Source as DragAndDropListViewTableSource).FormsElementWeakReference.TryGetTarget(out targetListView); var targetCollection = targetListView.ItemsSource as IList; NSIndexPath indexPath, destinationIndexPath; if (coordinator.DestinationIndexPath != null) { indexPath = coordinator.DestinationIndexPath; destinationIndexPath = indexPath; } else { // Get last index path of table view var section = tableView.NumberOfSections() - 1; var row = tableView.NumberOfRowsInSection(section); destinationIndexPath = NSIndexPath.FromRowSection(row, section); } // ReSharper disable once PossibleUnintendedReferenceComparison // we want to check reference! if (_sourceTableView == tableView) { // sorting see DragAndDropListViewTableSource.MoveRow } else { if ((int)destinationIndexPath.Item > targetCollection.Count) { targetCollection.Add(_dragItem); sourceCollection.Remove(_dragItem); } else { targetCollection.Insert((int)destinationIndexPath.Item, _dragItem); sourceCollection.Remove(_dragItem); } } }
public void PerformDrop(UITableView tableView, IUITableViewDropCoordinator coordinator) { var destinationIndexPath = coordinator.DestinationIndexPath; if (destinationIndexPath.Row >= albums.Count) { return; } switch (coordinator.Proposal.Operation) { case UIDropOperation.Copy: LoadAndInsertItems(destinationIndexPath, coordinator); break; case UIDropOperation.Move: break; default: return; } }
/// <summary> /// his delegate method is the only opportunity for accessing and loading /// the data representations offered in the drag item.The drop coordinator /// supports accessing the dropped items, updating the table view, and specifying /// optional animations.Local drags with one item go through the existing /// `tableView(_:moveRowAt:to:)` method on the data source. /// </summary> public void PerformDrop(UITableView tableView, IUITableViewDropCoordinator coordinator) { NSIndexPath indexPath, destinationIndexPath; // TODO: confirm this port is accurate if (coordinator.DestinationIndexPath != null) { indexPath = coordinator.DestinationIndexPath; destinationIndexPath = indexPath; } else { // Get last index path of table view var section = tableView.NumberOfSections() - 1; var row = tableView.NumberOfRowsInSection(section); destinationIndexPath = NSIndexPath.FromRowSection(row, section); } coordinator.Session.LoadObjects <NSString>((items) => { // Consume drag items List <string> stringItems = new List <string>(); foreach (var i in items) { var q = NSString.FromHandle(i.Handle); stringItems.Add(q.ToString()); } var indexPaths = new List <NSIndexPath>(); for (var j = 0; j < stringItems.Count; j++) { var indexPath1 = NSIndexPath.FromRowSection(destinationIndexPath.Row + j, destinationIndexPath.Section); model.AddItem(stringItems[j], indexPath1.Row); indexPaths.Add(indexPath1); } tableView.InsertRows(indexPaths.ToArray(), UITableViewRowAnimation.Automatic); }); }
/// <summary> /// Performs the drop. /// </summary> /// <param name="tableView">Table view.</param> /// <param name="coordinator">Coordinator.</param> public void PerformDrop(UITableView tableView, IUITableViewDropCoordinator coordinator) { var destinationIndexPath = coordinator.DestinationIndexPath; if (destinationIndexPath == null) { return; } coordinator.Session.LoadObjects <NSString>(items => { var path = items[0].ToString().Split(new char[] { ',' }, StringSplitOptions.None).Select(x => int.Parse(x)).ToList(); var secIdx = path[0]; var rowIdx = path[1]; var section = Element.Model.GetSection(secIdx); var destSection = Element.Model.GetSection(destinationIndexPath.Section); if (!destSection.UseDragSort) { return; } // save scroll position var offset = Control.ContentOffset; var fromCell = Control.CellAt(NSIndexPath.FromRowSection(rowIdx, secIdx)); if (section.ItemsSource == null) { // Don't use PerformBatchUpdates. Because can't cancel animations well. Control.BeginUpdates(); var cell = section.DeleteCellWithoutNotify(rowIdx); destSection.InsertCellWithoutNotify(cell, destinationIndexPath.Row); Control.DeleteRows(GetPaths(secIdx, rowIdx, 1), UITableViewRowAnimation.None); Control.InsertRows(GetPaths(destinationIndexPath.Section, destinationIndexPath.Row, 1), UITableViewRowAnimation.None); Control.EndUpdates(); Element.SendItemDropped(destSection, cell); } else { // Don't use PerformBatchUpdates. Because can't cancel animations well. Control.BeginUpdates(); var deletedSet = section.DeleteSourceItemWithoutNotify(rowIdx); destSection.InsertSourceItemWithoutNotify(deletedSet.Cell, deletedSet.Item, destinationIndexPath.Row); Control.DeleteRows(GetPaths(secIdx, rowIdx, 1), UITableViewRowAnimation.None); Control.InsertRows(GetPaths(destinationIndexPath.Section, destinationIndexPath.Row, 1), UITableViewRowAnimation.None); Control.EndUpdates(); Element.SendItemDropped(destSection, deletedSet.Cell); } // Cancel animations and restore the scroll position. var toCell = Control.CellAt(destinationIndexPath); toCell?.Layer?.RemoveAllAnimations(); fromCell?.Layer?.RemoveAllAnimations(); Control.Layer.RemoveAllAnimations(); Control.SetContentOffset(offset, false); // nothing occur, even if use the following code. //coordinator.DropItemToRow(coordinator.Items.First().DragItem, destinationIndexPath); }); }