简体   繁体   English

回忆一下 collectionView 上的 didSelectItemAt

[英]Recall didSelectItemAt on collectionView

In my project, there is label inside the collectionView cell that when user taps on it, the color of the label will change I did that with these codes在我的项目中,collectionView 单元格中有 label,当用户点击它时,label 的颜色会改变我用这些代码做到了

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      if let cell = collectionView.cellForItem(at: indexPath) as? CaseCell {
         cell.selectCell()
      }
   }
   
   public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
      if let cell = collectionView.cellForItem(at: indexPath) as? CaseCell {
         cell.deSelectCell()
      }
   }

Now, I want when user open this page and he/she already tap on any of them before, the selectCell will be called again.现在,我希望当用户打开此页面并且他/她之前已经点击过其中任何一个时,将再次调用selectCell I can say in the cellForRowAt if the cell is the selected cell, call that function, but it wont behave like didSelectItemAt and didSelectItemAt .我可以在cellForRowAt中说,如果单元格是选定的单元格,则调用 function,但它的行为不会像didSelectItemAtdidSelectItemAt So how can I set when the page gets opened, it call didSelectItemAt and tap on the specific index?那么如何设置页面打开时,它调用didSelectItemAt并点击特定索引? Then I when I add on the another cell, didSelectItemAt is also called.然后,当我添加另一个单元格时,也调用了 didSelectItemAt。 Thank you so much太感谢了


here is my sulation, I don't know it's acceptable or not这是我的建议,我不知道它是否可以接受

for example, I set the selected row to index.row == 2例如,我将所选行设置为 index.row == 2

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(reusableType: CaseCell.self, indexPath: indexPath)
      cell.configure(text: cases[indexPath.row])
      if indexPath.row == 2 {
         cell.selectCell()
      } else {
         cell.deSelectCell()
      }
      return cell
   }
   
   public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      if let cell = collectionView.cellForItem(at: indexPath) as? CaseCell {
         
         for index in 0..<cases.count {
            let index = IndexPath(row: index, section: 0)
            let deselctCell = collectionView.cellForItem(at: index) as? CaseCell
            deselctCell?.deSelectCell()
         }
         
         cell.selectCell()
      }
   }

A better approach would be to use both didSelect and didDeselect method to handle the selection and deselection of cells instead of interating through the entire collection of cells.更好的方法是同时使用didSelectdidDeselect方法来处理单元格的选择和取消选择,而不是遍历整个单元格集合。

public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    if let cell = collectionView.cellForItem(at: indexPath) as? CaseCell {
        cell.deSelectCell()
    }
}

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let cell = collectionView.cellForItem(at: indexPath) as? CaseCell {
        cell.selectCell()
    }
}

My suggestion is to create a property for the currently selected index path.我的建议是为当前选择的索引路径创建一个属性。 You can set it to a default value您可以将其设置为默认值

var selectedIndexPath : IndexPath? = IndexPath(row: 2, section: 0)

The most reliable way is to modify the cells only in cellForItemAt , nowhere else.最可靠的方法是cellForItemAt中修改单元格,其他任何地方都没有。 Depending on selectedIndexPath select or deselect the sell.取决于selectedIndexPath select 或取消选择出售。

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   let cell = collectionView.dequeueReusableCell(reusableType: CaseCell.self, indexPath: indexPath)
   cell.configure(text: cases[indexPath.row])
   indexPath == selectedIndexPath ? cell.selectCell() : cell.deSelectCell()
   return cell
}

In didSelectItemAt change the value of selectedIndexPath and reload both the current and the previous cell.didSelectItemAt中更改selectedIndexPath的值并重新加载当前和上一个单元格。

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
{
    if selectedIndexPath != indexPath  {
        let indexPathsToReload = selectedIndexPath == nil ? [indexPath] : [selectedIndexPath!, indexPath]
        selectedIndexPath = indexPath
        collectionView.reloadItems(at: indexPathsToReload)
    } else {
        selectedIndexPath = nil
        collectionView.reloadItems(at: [indexPath])
    }
}

didDeselectedItemAt is not needed. didDeselectedItemAt

The code considers also the case of an empty selection.该代码还考虑了空选择的情况。 If you don't want this declare selectedIndexPath as non-optional如果您不希望此声明selectedIndexPath为非可选

var selectedIndexPath = IndexPath(row: 2, section: 0)

and change didSelectItemAt to并将didSelectItemAt更改为

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
{
    guard selectedIndexPath != indexPath else { return }
    let indexPathsToReload = [selectedIndexPath, indexPath]
    selectedIndexPath = indexPath
    collectionView.reloadItems(at: indexPathsToReload)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CollectionView didSelectItemAt 没有被调用 - CollectionView didSelectItemAt is not getting called collectionView didSelectItemAt indexPath 未调用 swift - collectionView didSelectItemAt indexPath not called swift 没有调用collectionView didSelectItemAt indexPath - collectionView didSelectItemAt indexPath not being called 与UITapGestureRecognizer一起使用时,不调用collectionView的didSelectItemAt - collectionView's didSelectItemAt not called when use it with UITapGestureRecognizer Swift 3:CollectionView didSelectItemAt获取选定的单元格标签 - Swift 3: CollectionView didSelectItemAt get selected cell label 如何在collectionView(_:didSelectItemAt :)处调用selectItemAt:函数? - How can call selectItemAt: function at collectionView(_:didSelectItemAt:)? collectionView:UICollectionView,didSelectItemAt indexPath:未执行IndexPath - collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath is not executed collectionView didSelectItemAt 超级视图没有选择正确的对象 - collectionView didSelectItemAt superview not selecting the correct Object collectionView函数didSelectItemAt indexPath将数据传递给下一个视图控制器 - collectionView function didSelectItemAt indexPath to pass data to next view controller 以编程方式从collectionView Cell didSelectItemAt推送到新的ViewController-Swift - Pushing to new ViewController from collectionView Cell didSelectItemAt programmatically - swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM