【问题标题】:IndexPath.row of CollectionView gets changed while scrolling滚动时 CollectionView 的 IndexPath.row 发生变化
【发布时间】:2022-01-22 05:46:13
【问题描述】:

我是 swift 新手,我面临的问题是我有一个高度为 2000 的集合视图。当我向下滚动到底部时,indexPath.row 会发生变化,因此逻辑会受到干扰。我知道这是由于单元格的可重用性而发生的,但我应该如何阻止这种情况。这就是我初始化 cellForItemAt 的方式。

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "analysis", for: indexPath) as? analysisCollectionViewCell

  if goalData.notesArr.count == 0 {
        cell?.viewNotes.isHidden = true
        cell?.constraintHeightNotesView.constant = 0
        self.constraintHeightMainView.constant = self.constraintHeightMainView.constant - 400

    }else{
        cell?.viewNotes.isHidden = false
        cell?.constraintHeightNotesView.constant = 470
    }

我正在更改一些组件(cell?.constraintHeightNotesView.constant)以及我的 mainView 的高度(self.constraintHeightMainView.constant)。

【问题讨论】:

  • 考虑 cellFortItemAt 为每个(可见)行调用一次,并将当前 indexPath 作为参数传递。不要停下来。
  • 你能详细解释一下吗。
  • 请阅读developer.apple.com/documentation/uikit/…或有关UITableView/UICollectionView的教程。

标签: swift swift3 uicollectionview uicollectionviewcell


【解决方案1】:

请编辑问题,以便我们可以看到您的完整代码

这个单元格初始化方法是正确的

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "analysis", for: indexPath) as? analysisCollectionViewCell

但我不明白您为什么需要 2000 的集合视图高度?

如果要设置全屏布局集合视图,您需要将集合视图的约束设置到 ViewController 主视图的边缘

如果您的集合视图有很多元素,您将能够滚动它。 此功能无需额外配置即可使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-06
    • 2021-09-21
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多