Question

I am trying to perform a segue (with added properties) to a detail view controller when a custom cell in my UICollectionView is highlighted. I am a little stuck on how to achieve this as I cannot use PerformSegue from the subclassed UICollectionViewSource, and I cannot seem to get the selected cell from the UICollectionView.

Here's an abridged version of what I have so far:

Collection Source:

public class ProductCollectionDataSource : UICollectionViewSource
{       
    public ProductCollectionDataSource()
    {
        Products = new List<FeedItem>();
    }

    public List<FeedItem> Products { get; set; }

    public override void ItemUnhighlighted(UICollectionView collectionView, NSIndexPath indexPath)
    {
        var cell = (MultiColumnCell)collectionView.CellForItem(indexPath);
        cell.Alpha = 1.0f;
        // Perform segue here, passing this cell's data...?
    }
}

UIViewController:

public partial class DashboardViewController : UIViewController
{
    private ProductCollectionDataSource _dataSource;

    public override void ViewDidLoad()
    {
        _dataSource = new ProductCollectionDataSource();
        CollectionView.Source = _dataSource;

        GetProducts();
    }

    private async void GetProducts()
    {
        _dataSource.Products = new List<FeedItem>(await API.FeedService.Get());
        CollectionView.ReloadData();
    }
}

So, how can I trigger the segue in the UIViewController, based on the selected cell in the UICollectionView?

Was it helpful?

Solution

You could pass in a reference to your controller and then use that to do the Segue:

public class ProductCollectionDataSource : UICollectionViewSource
{
        WeakReference<DashboardViewController>  _dvc;

        public List<FeedItem> Products { get; set; }

        public ProductCollectionDataSource(DashboardViewController parentVc)
        {
            Products = new List<FeedItem>();

            _dvcRef = new WeakReference<DashboardViewController>(parentVc);
        }

        public override void ItemUnhighlighted(UICollectionView collectionView, NSIndexPath indexPath)
        {
            var cell = (MultiColumnCell)collectionView.CellForItem(indexPath);

            cell.Alpha = 1.0f;

            if (_dvcRef.TryGetTarget(out DashboardViewController dashboardVc){
                dashboardVc.PerformSegue("Identifier"); 
            }
        }
    }
}

OTHER TIPS

  1. Set your segue in storyboard (drag & drop)
  2. Open the right panel to add a segue identifier (basically a string like "CustomSegue")
  3. Use the collectionView delegate (didSelectItemAtIndexPath:) to trigger the user tap on a cell
  4. call [self performSegueWithIdentifier:@"CustomSegue" owner:self]
  5. Implement the method prepareForSegue in the controller that manages the UICollectionVIew
  6. Check if [segue.identifier isEqualToString:@"CustomSegue"]
  7. If so, then get the segue.destinationViewController (which is supposed to be your DetailViewController
  8. Pass any property you wish (segue.destinationViewController.property = propertyIWantToPass)

Manually Segue from your ViewController to the Detail View Controller. SetUp the Segue in the -(void)prepareForSegue method as usual.

Then call it manually where- and whenever you see fit:

-(void)yourCall{
    [self performSegueWithIdentifier:@"yourSegueIdentifier" sender:self];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top