2016-12-11 9 views
0

내 viewview 셀에서 다른 viewcontroller의 다른 collectionview 셀로 segue를 만들고 싶습니다. 예를 들어 첫 번째 셀을 두드리면 다른 viewcontroller에서 다른 collectionview의 첫 번째 셀로 이동하려고합니다. 또는 두 번째 셀을 두드리면 두 번째 셀로 가고 싶습니다. 내가 어떻게 이걸 얻을 수 있니?어떻게 collectionview 셀에서 다른 collectionview 셀로 segue를 만들 수 있습니까?

답변

0

하나의 viewcontroller에서 컬렉션 뷰 셀의 segue를 직접 다른 뷰 컨트롤러의 다른 셀로 이동할 수는 없습니다. 그러나 그것은 다음과 유사한 수행하여 당신은 당신이 원하는 것을 달성 할 수 있어야한다 :

부모의 ViewController

1

)를 NSIndexPath

스토리 보드 2) 설정 (또는 년을 유형의 속성 selectedIndexPath 추가 이 뷰 컨트롤러가 UICollectionViewDelegate

3) 컬렉션보기 위해 didSelectItemAtIndexPath 위임 메소드를 구현을 구현하는 코드)

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    selectedIndexPath = indexPath 
    [self performSegueWithIdentifier:@"identifier" sender:self]; 
} 

4) NSIndexPath에게 2보기 컨트롤러

-(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) 유형의 속성 initialDisplayCellIndexPath 추가로

2) 재정의 viewWillAppear

을 selectedIndexPath 값을 전달

(이것은 사용자의 collectionV 셀을 클릭의 결과로 코드 트리거 - iew 속성은

[self.view layoutIfNeeded]; 
[self.collectionView scrollToItemAtIndexPath:initialDisplayCellIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically 

는 기본적으로 어떤 위의 두 번째 뷰 컨트롤러에 하나의 뷰 컨트롤러에서 정상 SEGUE을하고하고있다) collectionView 지정됩니다. prepareForSegue의 일부로 두 번째보기 컨트롤러 (이 경우 선택한 항목의 indexPath 임)에 데이터를 전달할 수 있습니다. 그런 다음 두 번째 viewcontroller가 표시되면 콜렉션 뷰에 우리가 가고 싶은 곳으로 스크롤하도록 지시합니다.

희망 사항 :-)

+0

답장을 보내 주셔서 감사합니다. 네가 말한 모든 것을 다했는데 내가 셀을 가볍게 때리면 부서진다. viewWillAppear 내의 scrollToItemAtIndexPath 부분을 주석 처리 할 때 segue는 작동하지만 모든 셀이 두 번째보기 컨트롤러의 첫 번째 셀로 이동합니다 –

+0

이것은 오류입니다 : 캐치되지 않은 예외 'NSInvalidArgumentException'으로 인한 응용 프로그램 종료 중 이유 : '스크롤 시도 잘못된 인덱스 경로 :

관련 문제