Swift - 我的收藏 View 没有输出额外的数量

标签 swift uicollectionview

此处 chosenPlanArray.count 的计数为 5,这就是我的 Collection View 最终拥有的单元格数量。

但是,当我尝试将 Collection View 中的值附加到数组时,我的 Collection View 对自身进行了额外的迭代次数——在本例中,它达到了 9 次迭代。即使“return chosenPlayArray.count”是 5。这是非常随机的,我整天都在玩我的代码试图修复它。有人遇到过这种情况吗?

 public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {


    return chosenPlanArray.count

}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {



    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomPlanItCollectionViewCell



    cell.planImage.downloadedFrom(link: chosenPlanArray[indexPath.item].imageForPlan!)
    cell.planLbl.text = chosenPlanArray[indexPath.item].nameOfEvent
    cell.dateTimeLbl.text = chosenPlanArray[indexPath.item].eventStartsAt



    if cell.dateTimeLbl.text == nil {
            datesForFirebase = [""]
    } else {

        datesForFirebase.append(cell.dateTimeLbl.text!)


    }


    individualPlanNames.append(cell.planLbl.text!)




     imagesOfPlan.append(chosenPlanArray[indexPath.row].imageForPlan!)



    print(chosenPlanArray.count)

    print(imagesOfPlan.count)
    print(nameOfEventPlan.count)
    print(individualPlanNames.count)
    return cell
}

这是我在底部打印语句的输出。请注意第一个打印语句是如何正确的 - Collection View 的实际大小是 5,正如您可以通过秒中的 numberOfitems 看出的那样。我发现其他打印语句如何从 1 开始,然后循环超过的大小很奇怪'chosenPlanArray.count' - 问题是我可能将值附加到数组的方式吗?这导致我将错误数量的值上传到我的 firebase。所以我需要弄清楚这个棘手的情况。谢谢

5 // first print statement print(chosenPlanArray.count)
1 // second print statement print(imagesOfPlan.count)
1 // fourth  print(nameOfEventPlan.count)
1 // third print(individualPlanNames.count)

5
2
2
2

5
3
3
3

5
4
4
4

5
5
5
5
5
6
6
6
5
7
7
7
5
8
8
8
5
9
9
9

最佳答案

你必须知道有 cell reusing ,这意味着每次滚动都会调用 cellForItemAt ,这意味着这些行再次运行

individualPlanNames.append(cell.planLbl.text!)

imagesOfPlan.append(chosenPlanArray[indexPath.row].imageForPlan!)

并且他们的内容的大小增加了

关于Swift - 我的收藏 View 没有输出额外的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50619291/

相关文章:

ios - 安装 twilio iOS SDK 时出错

swift - Firestore 中的复合查询

ios - UIViewController Controller 无法识别委托(delegate)

ios - 单元格大小更改后,如何自动调整 CollectionViewCell 中内容的大小?

ios - 在 Swift 应用程序中使用数据库来管理志愿者

swift - WKWebView 中无法下载 PDF

ios - 在 [UIViewControllerTransitionCoordinator animateAlongsideTransition] 中指定动画选项

ios - 嵌入 UITabBarController 时 UICollectionViewCells 未加载

ios - 归档 IOS 应用程序时出现代码设计错误

iphone - 自定义 UICollectionView 布局