ios - 单击 UICollectionView 中的自定义单元格时如何触发 segue

标签 ios xamarin.ios xamarin

当我的 UICollectionView 中的自定义单元格突出显示时,我正在尝试对详细 View Controller 执行 segue(具有添加的属性)。我对如何实现这一目标有点困惑,因为我无法使用子类 UICollectionViewSource 中的 PerformSegue,而且我似乎无法从 UICollectionView< 中获取选定的单元格

这是我目前所拥有内容的删节版:

收集来源:

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();
    }
}

那么,我如何根据 UICollectionView 中选定的单元格在 UIViewController 中触发 segue?

最佳答案

您可以传入对 Controller 的引用,然后使用它来执行 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"); 
            }
        }
    }
}

关于ios - 单击 UICollectionView 中的自定义单元格时如何触发 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22690663/

相关文章:

ios - 在将快速分类数据用作目标分类时未得到自动建议

cocoa-touch - 测试应用内购买时使用哪个帐户?

c# - Xamarin.iOS 如何在没有导航栏时更改状态栏颜色

Xamarin - ITMS-90683 : Missing Purpose String in Info. plist

java - Opencv xamarin findContours - 如何使用层次结构

c# - Entry Xamarin Forms PCL 上的边框半径

ios - 恢复与 iPhone 的连接并再次运行 watchkitextension

ios - 将缩略图添加到 MKPinAnnotation

c# - Monotouch : Setting Window in pre 5. 0 设备

xaml - 无法让 Xamarin Xaml Intellisense 在 VS 2015 中工作