滚动UICollectionView时获取不正确的索引号

时间:2019-01-19 05:37:06

标签: ios swift uicollectionview uikit indexpath

我正在构建一个抽认卡应用,您可以从一个集合视图中选择一副纸牌,并根据所显示的具有副牌标题图像的单元格,在另一个收藏视图中为您显示特定的副牌。

我有一个viewController,它返回10个单元作为要选择的牌组,还有一个用于持卡人的viewController,其单元数取决于要返回的牌组。这些viewController都有一个单元格类。我无法根据我选择的卡座想出一种方法来返回正确的卡和单元格计数。

我试图做的是告诉它根据indexPath.item返回一个特定的字符串,并将其用作图像名称。除了indexPath.item没有按预期返回并且我得到错误的卡片组之外,这行得通,所以似乎我误解了indexPath.item的确定方式,并且我无法提出有效的解决方案来返回正确的甲板。

查看图像- Image of code and printed results 在单元格滚动浏览后,您会看到索引号以奇怪的方式打印。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! HomeViewCell

    collectionView.bounces = false

    let imageNumber = indexPath.item

    getNumber(imageNumber: imageNumber)
    let image = collectionImageArray[imageNumber].withRenderingMode(.alwaysOriginal)
    cell.collectionImage.setImage(image, for: .normal)
    cell.collectionImage.addTarget(self, action: #selector(handleCollectionTap), for: .touchUpInside)

    print(imageNumber)

    return cell
}

当我向下滚动到第二个单元格时,索引号从0跳到2并继续3、4、5 ...如果我从索引4向上滚动到3,它会打印出我从3跳下到0。

0 个答案:

没有答案