2016-11-24 76 views
3

我們有一個集合視圖。我做了一個水平顯示日期的日曆&我水平滾動日期。現在在屏幕上我只看到3-4個日期。現在我想在顯示日曆屏幕時自動滾動到特定的選定日期。因此,我想要滾動到的日期不可見。如何在swift中滾動到集合視圖中的某個特定索引

爲此我得到了特定單元格的索引路徑。現在我正在試圖將其滾動到特定的indexpath。

func scrollCollectionView(indexpath:IndexPath) 
    { 
    // collectionView.scrollToItem(at: indexpath, at: .left, animated: true) 
    //collectionView.selectItem(at: indexpath, animated: true, scrollPosition: .left) 
    collectionView.scrollToItem(at: indexpath, at: .centeredHorizontally, animated: true) 
    _ = collectionView.dequeueReusableCell(withReuseIdentifier: "DayCell", for: indexpath) as? DayCell 

    } 

請告訴我該如何實現它?

+1

看起來像調用scrollCollectionView有問題。你從哪裏調用這個方法? –

+0

就在我的收藏視圖準備就緒後 – Techiee

+0

如果您將代碼發佈到創建,準備並最終添加收藏視圖供我們查看的地方,將會有所幫助。 –

回答

3

您可以使用此

self.collectionView.scrollToItem(at:IndexPath(item: index, section: 0), at: .right, animated: false)

10

在控制器的viewDidLoad,您可以編寫代碼滾動集合視圖的特定索引路徑。

self.collectionView.scrollToItem(at:IndexPath(item: indexNumber, section: sectionNumber), at: .right, animated: false) 
0

我用你的答案@PGDev,但我把它放在viewWillAppear,它對我很好。

self.collectionView?.scrollToItem(at:IndexPath(item: yourIndex, section: yourSection), at: .left, animated: false) 
相關問題