【问题标题】:Wrong IndexPath of my UICollectionView我的 UICollectionView 的 IndexPath 错误
【发布时间】: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.sectionindexPath.row计算索引
  • 在我之前的评论中,忘记添加当前索引:index += indexPath.row
  • 一个indexPath 有一个section 和一个row(用于表格视图)或item(用于集合视图)值,这两个索引一起是您的索引路径,它清楚地标识当前集合中的每个元素都清楚。
  • 谢谢你,你的回答帮助很大!我解决了我的问题。

标签: ios swift indexing uicollectionview uicollectionviewcell


【解决方案1】:

IndexPath 为单元格提供有关其在collectionView 中的位置的信息。单元格索引可以通过NSIndexPath 中的sectionitem 属性来标识。使用这些属性的组合来确定确切的位置。

【讨论】:

    【解决方案2】:

    你可以在每个列表中使用tags,并在需要获取值时检查它,例如

    if (list.tag == 0 && indexPath.item = 0){
       //list 1 and indexPath.item is 0
    } 
    
    if (list.tag == 1 && indexPath.item = 0){
       //list 2 and indexPath.item is 10
    }
    

    我不知道有没有办法直接获得这个值,因为你正在创建一组新的CollcetionViewCell。您也可以根据部分进行跟踪。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多