[英]How can I create a segue from a collectionview cell to another collectionview cell?
我想創建一個從我的collectionview單元到另一個viewcontroller的另一個collectionview單元的序列。 例如,當點擊第一個單元格時,我想從另一個視圖控制器轉到另一個collectionview的第一個單元格。 或者,當點擊第二個單元格時,我想轉到第二個單元格。 我怎樣才能做到這一點?
您不能直接從一個viewcontroller中的集合視圖單元轉到另一個視圖控制器中的另一個單元的segue。 但是,可以通過執行以下類似操作來實現所需的目標:
父級ViewController
1)添加屬性NSIndexPath類型的selectedIndexPath
2)在情節提要(或代碼)中設置此視圖控制器實現UICollectionViewDelegate
3)為您的集合視圖實現didSelectItemAtIndexPath委托方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndexPath = indexPath
[self performSegueWithIdentifier:@"identifier" sender:self];
}
4)將selectedIndexPath值傳遞給第二視圖控制器
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// :: Should do other checking - if we have segues to other screens
SecondViewController *secondViewController = (SecondViewController *)segue.destinationViewController;
secondViewController.initialDisplayedCellIndexPath = selectedIndexPath
}
}
SecondViewController代碼
1)添加NSIndexPath類型的屬性initialDisplayCellIndexPath
2)覆蓋viewWillAppear
(這是假設您的collectionView屬性命名為collectionView)
[self.view layoutIfNeeded];
[self.collectionView scrollToItemAtIndexPath:initialDisplayCellIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically
基本上,上面的操作是從一個視圖控制器到第二個視圖控制器進行正常的篩選-由於單擊單元格而在代碼中觸發。 作為prepareForSegue的一部分,我們可以將數據傳遞到第二個視圖控制器(在這種情況下,它是所選項目的indexPath)。 然后,當顯示第二個viewcontroller時,我們告訴集合視圖滾動到我們要去的地方。
希望有道理:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.