簡體   English   中英

scrollToItem 在我的 CollectionView 中不起作用?

[英]scrollToItem isn't working in my CollectionView?

為什么 scrollToItem 在我的 CollectionView 中不起作用? 我想在EnabledID上工作 scrollToItem 。 那么什么是問題以及如何解決呢?

代碼:

var dataArray = NSMutableArray() // dataArray is has a data from Database
var EnabledID = Int()

EnabledID = UserDefaults.standard.integer(forKey: "EnabledID")
    
if EnabledID == 0 {
        
    EnabledID = 1
    UserDefaults.standard.set(1, forKey: "EnabledID")
        
} else {
    if EnabledID < dataArray.count {
        let index = NSIndexPath(item: EnabledID, section: 0)
        self.myCollectionView.scrollToItem(at: index as IndexPath, at: .centeredVertically, animated: true)
    }
}
    

試試這個代碼...

[collectionView setDelegate:self];
[collectionView reloadData];
[collectionView layoutIfNeeded];
[collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];

斯威夫特 - 4.x

collectionView.delegate = self
collectionView.reloadData()
collectionView.layoutIfNeeded()
collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: true)

當滾動方向為水平時,您需要使用leftrightcenteredHorizo​​ntally

頂部是垂直方向。

collectionView.scrollToItem(at: index, at: .top, animated: true)

我發現這樣做的最好方法是不使用 scrollToItem 而是獲取索引的 CGRect 然后使其可見。

 let rect = self.collectionView.layoutAttributesForItem(at: IndexPath(row: 5, section: 0))?.frame
 self.collectionView.scrollRectToVisible(rect!, animated: false)

我的問題有點奇怪。 我的 iPhone 6/7/8 工作正常,但 iPhone X 或 11 無法滾動到我的預期位置。 我的原始代碼是:

collectionView.reloadData()
collectionView.scrollToItem(at: IndexPath(item: 10, section: 0), at: .top, animated: true)

在我添加 collectionView.layoutIfNeeded() 之后。 問題在我的 iPhone X 和 11 上得到解決。

collectionView.reloadData()
collectionView.layoutIfNeeded()
collectionView.scrollToItem(at: IndexPath(item: 10, section: 0), at: .top, animated: true)

這個答案的靈感來自@ram。 他應該得到榮譽。 添加這個答案只是為了強調不同iPhone之間的區別。 因此,人們可以更快地搜索到他們的答案。

它不起作用,因為 contentSize 可能是 (0,0)。 如果您不想使用 layoutIfNeeded() (因為您可能會在那里執行一些不需要的不必要的東西),您應該自己計算它(假設您的數據已加載,並且視圖進行了布局)。

斯威夫特 5.2

    collection.contentSize = CGSize(width: collection.bounds.width * CGFloat(items.count), height: collection.bounds.height)
    collection.scrollToItem(at: IndexPath(row: startingIndex, section: 0), at: .left, animated: false)

試試這個水平集合視圖

var isViewDidLayoutCallFirstTime = true


override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    guard self.isViewDidLayoutCallFirstTime else {return}
    self.isViewDidLayoutCallFirstTime = false
    self.collectionView.collectionViewLayout.collectionViewContentSize // It will required to re calculate collectionViewContentSize in internal method 
    let indexpath = IndexPath(item: self.currentIndex, section: 0)

    self.collectionView.scrollToItem(at: indexpath, at: UICollectionViewScrollPosition.centeredHorizontally, animated: false)

}

我在 iOS 14.x 和 Xcode 12.x 上遇到過這個問題。

對我有用的解決方案是使用:

collectionView.scrollRectToVisible(rect, animated: true)

在我的情況下 .centeredVertically 沒有任何效果,並且正在悄悄地失敗。 .centeredHorizo​​ntally 固定它

試試這個。它對我有用

func scrollToIndex(index:Int) {
     let rect = self.collectionView.layoutAttributesForItem(at:IndexPath(row: index, section: 0))?.frame
     self.collectionView.scrollRectToVisible(rect!, animated: true)
 }
 var scrollImg: Int = 0

override func viewDidLoad() {
    super.viewDidLoad()
    _ = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(autoScroll), userInfo: nil, repeats: true)

}

   @objc func autoScroll() {
    
    let totalCount = imageArray.count-1
    
    if scrollImg == totalCount{
        scrollImg = 0
    }else {
        scrollImg += 1
    }
    
    DispatchQueue.main.async {
        let rect = self.listOfViewsCollectionViews.layoutAttributesForItem(at: IndexPath(row: self.scrollImg, section: 0))?.frame
        if self.scrollImg == 0{
            self.listOfViewsCollectionViews.scrollRectToVisible(rect!, animated: false)
        }else {
            self.listOfViewsCollectionViews.scrollRectToVisible(rect!, animated: true)
        }
    }
}
collectionView.collectionViewLayout.invalidateLayout()
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
collectionView.collectionViewLayout.invalidateLayout()

我使用這樣的解決方法:

collectionView.isPagingEnabled = false
collectionView.scrollToItem(
    at: /* needed IndexPath */,
    at: .centeredHorizontally,
    animated: true
)
collectionView.isPagingEnabled = true

我在這里這里的幾篇文章中找到了這樣的解決方案。

用這個

self.messageCV.reloadData()
let indexPath = IndexPath(row: 0, section: 0)
self.messageCV.scrollToItem(at: indexPath, at: .bottom, animated: true) // .top

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM