UICollectionView - 查找当前索引路径并更改值/数据

时间:2017-10-13 15:54:02

标签: ios swift uicollectionview uicollectionviewcell

我试图找出如何捕获集合视图的当前索引路径。

我正在使用横​​向流程,我已经说明了以下内容:

  func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

有三个预定义的单元格,我希望相应地更改三个集合视图单元格中的每一个上的文本/ UI元素。我怎样才能做到这一点?我真的很挣扎,因为目前我正在获得3个充满相同数据的单元格。这是预期的。

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

               let cell = detailsCollectionView.dequeueReusableCell(withReuseIdentifier: "DetailsCollectionViewCell", for: indexPath) as! DetailsCollectionViewCell
                cell.layer.shadowRadius = 1
                cell.layer.shadowOpacity = 0.1
                cell.layer.shadowOffset = CGSize(width: 2.5, height: 5)

                return cell
        }
    }
}

但实质上我希望执行以下操作,伪代码:

    if cell.indexPath == 0 {
      label.text = "This is the first cell"

    } else if cell.indexPath == 1 {
     label.text = "This is the second cell"

    } else if cell.indexPath == 2 {
     label.text = "This is the third cell"
   }
}

如果这是一个愚蠢的问题,请道歉。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

您应该比较item的{​​{1}}参数的indexPath属性。

cellForItemAt