【发布时间】:2014-10-31 13:12:31
【问题描述】:
我正在尝试调用collectionView...scrollToItemAtIndexPath 将我的UICollectionView 滚动到第i 个单元格,其中i 是元素的索引。我的集合视图只有一个部分,当我离开另一个部分时,我总是调用dataArray[indexPath.item] 来获取 indexPath 中元素的数据。但是,我无法从整数中获取 indexPath。
[NSIndexPath indexPathForRow:] 仅适用于 UITableView
[NSIndexPath indexPathWithIndex:] 不返回带有部分的NSIndexPath
[NSIndexPath indexPathForItem:inSection:0] 返回一个长度为 0 的路径,当用于获取具有 [collectionView cellAtIndexPath:] 的单元格时,该路径返回 nil。
知道我能做些什么来获得一个有效的索引路径,然后我可以用它来滚动我的UICollectionView? 假设我只有数据数组的NSInteger 索引。谢谢。
使用代码更新:
NSIndexPath* pathToCell = [NSIndexPath indexPathForItem:curIndex inSection:0];
CustomCollectionViewCell* updatedCell = (CustomCollectionViewCell*)[self collectionView:self.collection cellForItemAtIndexPath:pathToCell];
selectedContent = updatedCell.cellContentView;
// scroll to cell and recenter cover on it
[self.collection scrollToItemAtIndexPath:pathToCell atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
然后我使用单元格的位置在其顶部居中放置一个“封面”视图:
// Animate
CGPoint coverCenter = [childView convertPoint:selectedContent.center toView:parentView];
coverSelectDummy.center = coverCenter;
coverSelectDummy.imageView.image = selectedContent.imageView.image;
coverSelectDummy.imageLabel.text = selectedContent.imageLabel.text;
coverSelectDummy.alpha = 1.0;
coverSelectDummy.transform = CGAffineTransformIdentity;
coverSelectDummy.hidden = NO;
[self.view bringSubviewToFront:coverSelectDummy];
[UIView transitionWithView:coverSelectDummy duration:0.1f options: UIViewAnimationOptionCurveLinear animations:^(void)
{
coverSelectDummy.alpha = 1.0;
coverSelectDummy.transform = CGAffineTransformMakeScale(1.2, 1.2);
}
completion:^(BOOL finished)
{
// .... processing ....
}];
【问题讨论】:
-
你能说明你是如何使用
[NSIndexPath indexPathForItem:inSection:0]的吗?我想看看这个问题的所有相关代码 -
是的,如上所示。发生的事情是返回的 IndexPath 似乎存在,但它不会产生我作为整数传入的单元格。事实上,我看到的是封面视图转到集合视图的左上角,而不是特别是在任何单元格上,因为 selectedContent 是 nil
-
该代码不应该是
[self.collection cellForItemAtIndexPath:pathToCell];还是我很困惑?甚至不清楚[self collectionView:]是什么。 -
我同意 trojanfoe - 你的代码有错误 - 这很可能是错误。然后按照下面的答案就可以了。
标签: ios objective-c iphone uicollectionview nsindexpath