public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
        {
            ModelItem      item = this.items[(int)indexPath.Item];
            CollectionCell cell = null;

            if (item.picture != null)
            {
                cell = (CollectionCell)collectionView.DequeueReusableCell("CollectionImageCell", indexPath);
            }
            else
            {
                cell = (CollectionCell)collectionView.DequeueReusableCell("CollectionCell", indexPath);
            }

            if (cell == null)
            {
                cell = (CollectionCell) new UICollectionViewCell();
            }

            if (item.value != null)
            {
                cell.setText(item.value.ToString());
            }
            else
            {
                cell.setText(string.Empty);
            }

            if (item.picture != null)
            {
                cell.setPicture(item.picture);
            }
            return(cell);
        }
Example #2
0
        public void HandleTableLongPress(NSObject _sender)
        {
            UILongPressGestureRecognizer sender = (UILongPressGestureRecognizer)_sender;

            CGPoint tapLocation = sender.LocationInView(this.View);
            UIView  hitView     = this.View.HitTest(tapLocation, null);

            switch (sender.State)
            {
            case UIGestureRecognizerState.Began:
            {
                Console.WriteLine("Gesture Began");
                if (hitView.GetType() == typeof(MyCollectionView))
                {
                    sourceCollection = (MyCollectionView)hitView;
                    sourceIndexPath  = sourceCollection.IndexPathForItemAtPoint(sender.LocationInView(sourceCollection));
                    if (sourceIndexPath == null)
                    {
                        sender.Enabled = false;
                        sender.Enabled = true;
                        Console.WriteLine("Tap location does not point to any cell");
                        return;
                    }

                    CollectionCell sourceCell = (CollectionCell)sourceCollection.CellForItem(sourceIndexPath);
                    sourceCell.Alpha = 0.25f;
//							NSData cellViewMirrorData = NSKeyedArchiver.ArchivedDataWithRootObject(sourceCell);
//							this.draggableView = (UIView)NSKeyedUnarchiver.UnarchiveObject(cellViewMirrorData);

                    this.draggableView        = sourceCell.SnapshotView(true);
                    this.draggableView.Center = tapLocation;
                    this.draggableView.Alpha  = 0.5f;
                    this.draggableView.Hidden = false;
                    this.View.AddSubview(this.draggableView);
                }
            }
            break;

            case UIGestureRecognizerState.Changed:
            {
                this.draggableView.Center = tapLocation;
            }
            break;

            case UIGestureRecognizerState.Ended:
            {
                Console.WriteLine("Gesture Ended");

                this.draggableView.RemoveFromSuperview();
                this.draggableView.Dispose();
                this.draggableView = null;


                tapLocation = sender.LocationInView(this.View);
                hitView     = this.View.HitTest(tapLocation, null);

                CollectionCell sourceCell = (CollectionCell)sourceCollection.CellForItem(sourceIndexPath);

                if (hitView.GetType() != typeof(MyCollectionView) || hitView.IsEqual(sourceCollection))
                {
                    Console.WriteLine("Drop location does not point to any collection");
                    sourceCell.Alpha = 1.0f;
                    return;
                }
                MyCollectionView destinationCollection = (MyCollectionView)hitView;
                ModelItem        item = ((CollectionViewDataSource)sourceCollection.DataSource).items[(int)sourceIndexPath.Item];

                NSIndexPath destinationIndexPath = destinationCollection.IndexPathForItemAtPoint(sender.LocationInView(destinationCollection));
                ((CollectionViewDataSource)destinationCollection.DataSource).addItemAtIndexPath(item, destinationIndexPath);

                ((CollectionViewDataSource)sourceCollection.DataSource).removeItemAtIndexPath(sourceIndexPath);
            }
            break;

            case UIGestureRecognizerState.Cancelled:
            {
                Console.WriteLine("Gesture Cancelled");
                if (this.draggableView != null)
                {
                    this.draggableView.RemoveFromSuperview();
                    this.draggableView.Dispose();
                    this.draggableView = null;
                }
                if (sourceIndexPath != null)
                {
                    CollectionCell sourceCell = (CollectionCell)sourceCollection.CellForItem(sourceIndexPath);
                    sourceCell.Alpha = 1.0f;
                }
            }
            break;
            }
        }