【发布时间】:2016-10-26 14:57:13
【问题描述】:
我的 UICollectionView 有问题。
我用 2 个列表创建了我的 collectionView。 2 个列表包含每个列表的标题。 例如,当我单击第一个列表中的第一个单元格时,我的代码会向我发送 indexPath.item = 0。一切正常。 但是当我点击第二个列表上的第一个索引时,我的索引也返回 0 ,但我更喜欢它在第一个列表之后。
例如,如果我的第一个列表中有 10 个项目 (indexPath 0 -> 9) 我希望我的第二个列表的第一个对象等于 to 10 并且不是从头开始!提前谢谢你。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let cell = sender as? UICollectionViewCell,
indexPath = collectionView?.indexPathForCell(cell),
managePageViewController = segue.destinationViewController as? ManagePageViewController {
managePageViewController.videoList = dataSource.pictureOfVideo
managePageViewController.currentIndex = indexPath.item
}
}
【问题讨论】:
-
NSUInteger index = 0;for (NSUInteger i = 0; i < targetIndexPath.section; i ++){index += [yourCollectionView numberOfItemsInSection:i];} managePageViewController.currentIndex = index;可以解决问题。 -
如果这两个列表是不同部分的一部分,您可以根据
indexPath.section和indexPath.row计算索引 -
在我之前的评论中,忘记添加当前索引:
index += indexPath.row -
一个
indexPath有一个section和一个row(用于表格视图)或item(用于集合视图)值,这两个索引一起是您的索引路径,它清楚地标识当前集合中的每个元素都清楚。 -
谢谢你,你的回答帮助很大!我解决了我的问题。
标签: ios swift indexing uicollectionview uicollectionviewcell