1

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

我有一个 viewController,它返回 10 个单元格作为要选择的牌组,还有一个用于卡片查看器的 viewController,其单元格计数取决于要返回的牌组。这些视图控制器中的每一个都有单元类。根据我选择的牌组,我无法找出返回正确卡片和单元格计数的方法。

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

查看图像 - 代码和打印结果的图像 在单元格滚动后,您会看到以奇怪的方式打印的索引号。

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。

4

0 回答 0