简体   繁体   English

UICollectionView scrollToItemAtIndexPath不起作用

[英]UICollectionView scrollToItemAtIndexPath doesn't work

[myCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];

myCollectionView is not nil, and contains 3 items. myCollectionView不是nil,包含3个项目。 I'm trying to scroll it programatically so that the 2nd item appears at the left edge of the collection view's frame. 我正在尝试以编程方式滚动它,以便第二个项目出现在集合视图框架的左边缘。

The collection view does not move at all. 集合视图根本不移动。 I've also tried setContentOffset , which also has no affect. 我也尝试过setContentOffset ,它也没有任何影响。 What would be preventing the collection view from scrolling? 什么会阻止集合视图滚动?

EDIT: setContentOffset does actually work. 编辑: setContentOffset确实有效。 I'd still be interested in knowing why scrollToItemAtIndexPath doesn't work though. 我仍然有兴趣知道为什么scrollToItemAtIndexPath不起作用。

I found this question because I had the same exact troubles and I was searching for related topics. 我找到了这个问题,因为我遇到了同样的麻烦,我正在寻找相关主题。 If you still need it, I can tell you setContentOffset shouldn't be needed. 如果你仍然需要它,我可以告诉你不应该需要setContentOffset The way I fixed it was to manually assign the yourCollectionView.scrollEnabled boolean property as true to allow it to scroll programmatically. 我修复它的方法是手动将yourCollectionView.scrollEnabled布尔属性指定为true以允许它以编程方式滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM