2013-04-13 39 views
76

在加載集合視圖之前,用戶設置集合視圖數組中圖像的數量。所有的單元格都不適合在屏幕上。我有30個單元格,屏幕上只有6個單元格。UICollectionView自動滾動到IndexPath處的單元格

問題:如何在UICollectionView的加載時自動滾動到所需圖像的單元格?

回答

136

新編輯的答案:

viewDidLayoutSubviews

-(void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews]; 
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO]; 
} 

舊答案添加這對老年人的iOS工作

viewWillAppear補充一點:

[self.view layoutIfNeeded]; 
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO]; 
+4

這正是我想要使用的。我有數組中的對象的編號,我知道該對象的名稱。但不知道如何把正確的indexPath ...任何想法? – AlexanderZ

+4

您可以輕鬆創建'indexPath':'[NSIndexPath indexPathForItem:numberOfTheObject切入口:sectionNumber]' –

+3

有一些錯誤。但後來我把所有提到的代碼放在(void)viewDidLayoutSubviews {}中,現在它工作正常。 Thanx Bogdan。 – AlexanderZ

13

我完全同意上面的答案。唯一的一點是,我將溶液設置代碼在viewDidAppear

viewDidAppear 
{ 
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES]; 
} 

當我使用的viewWillAppear中滾動沒有工作。

+0

我秒! – Mike

+1

viewDidAppear有你看到的集合視圖移動/閃爍,認爲已經出現(顧名思義)的缺點......我真的不知道爲什麼蘋果沒有設計一個更好的方式這並不那麼罕見的情況下,viewDidLayoutSubviews方式有點尷尬 – TheEye

68

我發現,在viewWillAppear中滾動因爲集合視圖還沒有完成它的佈局卻可能無法可靠地工作;您可能會滾動到錯誤的項目。

我還發現,在viewDidAppear滾動會導致視未渦卷的瞬間閃光可見。

而且,如果每次通過viewDidLayoutSubviews滾動,用戶將無法手動滾動,因爲某些集合佈局每次滾動時都會導致子視圖佈局。

這裏是我的發現工作可靠:

- (void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews]; 

    // If we haven't done the initial scroll, do it once. 
    if (!self.initialScrollDone) { 
     self.initialScrollDone = YES; 

     [self.collectionView scrollToItemAtIndexPath:self.myInitialIndexPath 
            atScrollPosition:UICollectionViewScrollPositionRight animated:NO]; 
    } 
} 
+0

好的提示。作爲倫克做了,我會強調使用'initialScrollDone'標誌,因爲這種方法將被調用一次以上,如果你打電話scrollToItemAtIndexPath:不止一次,似乎呈現的CollectionView unscrollable(的iOS模擬器iPhone 5/8的iOS) – maz

+1

THX ,這應該是被接受的答案 – Jakob

+1

在iOS8中很好用。應該是被接受的答案。 – Nick

11

這似乎爲我工作,它類似於另一種答案,但有一些明顯的區別:

- (void)viewDidLayoutSubviews 
{  
    [self.collectionView layoutIfNeeded]; 
    NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems]; 
    NSIndexPath *currentItem = [visibleItems objectAtIndex:0]; 
    NSIndexPath *nextItem = [NSIndexPath indexPathForItem:someInt inSection:currentItem.section]; 

    [self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionNone animated:YES]; 
} 
10

斯威夫特:

your_CollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true) 

Swift 3

let indexPath = IndexPath(row: itemIndex, section: sectionIndex) 

collectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.right, animated: true) 

滾動位置:

UICollectionViewScrollPosition.CenteredHorizontally/UICollectionViewScrollPosition.CenteredVertically 
7

您可以使用GCD到滾動派遣到主運行循環的viewDidLoad中的下一個迭代,以實現這種行爲。滾動將在收集視圖顯示在屏幕上之前執行,因此不會閃爍。

- (void)viewDidLoad { 
    dispatch_async (dispatch_get_main_queue(), ^{ 
     NSIndexPath *indexPath = YOUR_DESIRED_INDEXPATH; 
     [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO]; 
    }); 
} 
+0

這是唯一適合我的方法 –

0

這裏的所有答案 - 黑客。我已經找到更好的辦法來relaodData後滾動某處集合視圖:
什麼都佈局在使用 子類集合視圖佈局,覆蓋方法prepareLayout,超級呼叫建立什麼都偏移,您需要之後。
ex:https://stackoverflow.com/a/34192787/1400119

0

我會建議在collectionView: willDisplay:這樣做,然後你可以確定集合視圖委託提供了一些東西。 這裏我的例子:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
    /// this is done to set the index on start to 1 to have at index 0 the last image to have a infinity loop 
    if !didScrollToSecondIndex { 
     self.scrollToItem(at: IndexPath(row: 1, section: 0), at: .left, animated: false) 
     didScrollToSecondIndex = true 
    } 
} 
相關問題