当我的 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/