小编mox*_*iro的帖子

UIcollectionView scrollToitemAtIndexPath不适用于非可见单元格

我是iOS和Swift的新手.

设法使用自定义LayoutAttributes和可重复使用的自定义单元格创建UICollectionView,该单元格计算图像的高度.这一切都很棒.工作正常,我可以手动滚动.都好.

问题是我想以编程方式滚动到第N个indexPath,但scrollToItemAtIndexPath似乎只适用于可见的.

所以我需要2列图像,在iphone 5S上显示9张图像.图像总数为31.

因此,使用collectionView的ScrollToItemAtIndexPath可以处理第9项.

但是,当我尝试进一步以编程方式滚动时,比如12,它不起作用.奇怪的是,如果手动滚动,然后调用代码,它的工作原理.所以我调试了一下,看到只有前9个单元格的高度计算.那会是问题吗?如果是这样,我怎么能让它工作?

尝试过使用didLayoutSubviews和其他人的建议,但它没有用.

目前,我正在使用didSelectItemAtIndexPath collectionView功能以编程方式滚动.

编辑:

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let item = self.collectionView(self.collectionView!, numberOfItemsInSection: 0) - 1
            let lastItemIndex = NSIndexPath(forItem: item, inSection: 0)
            self.collectionView?.scrollToItemAtIndexPath(lastItemIndex, atScrollPosition: UICollectionViewScrollPosition.CenteredVertically, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

即使我硬编码索引路径的编号(例如12),它仍然不起作用.如果需要更多信息,请告诉我.

我在这里错过了什么吗?

非常感谢.

ios uicollectionview uicollectionviewcell swift

2
推荐指数
1
解决办法
2352
查看次数