2014-06-20 3 views
0

스토리 보드에서 단추를 연결하면 가로 스크롤 컬렉션을 스크롤 할 때 한 항목을 오른쪽으로 볼 수 있습니다.UICollectionView를 스크롤하는 버튼 만들기

- (IBAction)rightButton:(id)sender { 
    NSIndexPath *indexPath = [self.collectionView indexPathsForSelectedItems]; 
    NSIndexPath* newIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]; 


    [self.collectionView scrollToItemAtIndexPath:newIndexPath 
          atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally 
            animated:YES]; 

}

그러나,이 충돌을 만드는 나는 문제가이 코드는 더 많은 것을 의심

-[__NSArrayI row]: unrecognized selector sent to instance 0x178232540 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI row]: unrecognized selector sent to instance 0x178232540' 

오류와 함께 : 여기에 내가 생각 해낸 코드입니다 TableView와 거기에 섹션 공원을 좋아하지 않아.

누군가 나를 도와 주며 오류가 발생하는 이유를 알아 냈습니까? 이 작업을 수행하는 더 좋은 방법이 있습니까? 이 인덱스 경로가 아닌 단일 인덱스 경로의 NSArray을 반환을 나타냅니다

감사

답변

1

indexPathsForSelectedItems 메소드 이름이 거기에 추가 s을 가지고 Paths하지 Path을. 따라서 솔루션이

NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 

NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems]; 
if (indexPaths.count > 0) 
{ 
    NSIndexPath *oldIndexPath = indexPaths[0]; 
    NSInteger oldRow = oldIndexPath.row; 
    newIndexPath = [NSIndexPath indexPathForRow:oldRow + 1 inSection:oldIndexPath.section]; 
} 
+0

이봐 이러한 라인

NSIndexPath *indexPath = [self.collectionView indexPathsForSelectedItems]; NSIndexPath* newIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]; 

을 변경하는 것입니다, 그래서 나는 그에게 내 코드를 변경하고 거기 추가를 좋아하지 않는다. 이 아키텍처와 플랫폼에 대해 일정한 크기가 아닌''인터페이스 "id"에 대한 포인터에 산술 연산 오류가 발생했습니다. –

+1

@Deannakov - 예, 배열의 객체가' id'. 객체를'NSIndexPath * '에 대입 (그리고 암시 적으로 변환)하면 컴파일러를 행복하게 유지할 수 있습니다. 설명 할 답변을 업데이트했습니다. – user3386109

+0

Ok - 이렇게하면 선택한 항목의 오른쪽으로 한 항목 스크롤됩니다. 문제는 * 선택한 항목의 오른쪽에있는 항목을 선택 *하는 것이 아니라 그것이 내게 있다는 것입니다. 즉, 내 코드는 스크롤하는 것뿐이었습니다. 도와 주셔서 감사합니다! 변화를위한 선택을 얻는 것에 대한 통찰력이 있다면 그것은 좋은 연구이지만 약간의 연구를 할 것입니다. –

관련 문제