cellForItemAtIndexPath

cellForItemAtIndexPath

因此,我正在尝试编写一些代码,以将集合 View 滚动到某个索引,然后提取对单元的引用并执行一些逻辑。但是我注意到,如果在滚动之前当前尚不可见该单元格,则cellForItemAtIndexPath调用将返回nil,从而导致其余逻辑失败。

[_myView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index
                                                     inSection:0]
                atScrollPosition:UICollectionViewScrollPositionTop
                        animated:NO];

//Tried with and without this line, thinking maybe this would trigger a redraw
[_myView reloadData];

//returns nil if cell was off-screen before scroll
UICollectionViewCell *cell =
  [_myView cellForItemAtIndexPath:
    [NSIndexPath indexPathForItem:index inSection:0]];

我是否需要调用其他方法来使cellForItemAtIndexPath返回某个单元格的某些内容,而该单元格是由于紧挨其前的滚动而突然出现的?

最佳答案

根据您的评论,这听起来像是您最终要找到的单元格框架。在不依赖单元格存在的情况下,执行此操作的方法是询问集合 View 的布局:

NSIndexPath *indexPath = ...;
UICollectionViewLayoutAttributes *pose = [self.collectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath];
CGRect frame = pose.frame;

关于ios - 在强制滚动使其可见之后,cellForItemAtIndexPath返回nil,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21469459/

10-11 17:14