【问题标题】:UICollectionView with dynamic Section and Row iOS Swift 3.具有动态部分和行 iOS Swift 3 的 UICollectionView。
【发布时间】:2025-12-21 23:15:15
【问题描述】:

我在 iOS Swift 3 中有 collectionView。目前在其中 5 节和 5 行。这将是动态部分和行。在每一行和每一节我都有 textfield 。我的问题是如何在文本字段操作 func textFieldDidChange(textfieldChange: UITextField){} 中检测文本字段。我需要知道带有部分和行位置的文本字段标签。这样我就可以将文本字段值存储到单个数组中。当我检测到从 0 到 4 索引的每个部分的文本字段标记时。但是每个部分中的文本字段标记都会更改,并且每个部分中的文本字段标记都会初始化 0 到 4。这是我的文本字段操作函数

func textFieldDidChange(textfieldChange: UITextField){
      }

【问题讨论】:

  • 将目标添加到collectiveview单元格中的文本字段。给它标签,您将能够访问它。

标签: ios swift collectionview


【解决方案1】:

cellForRow中设置这样的按钮标签

let tag = indexPath.section*100 + indexPath.row
btnABC.tag = tag

在按钮操作中获取特定按钮的行和部分

let tag = sender.tag // make sure sender is button type.
let Row = tag % 100
let Section = (tag - Row)/100

【讨论】:

    【解决方案2】:

    您可以通过在您的文本字段的委托方法中使用以下代码来获取您的文本字段所在的 CollectionView 项目的部分和行值:

    let item:UICollectionViewCell = textField.superview?.superview as! UICollectionViewCell
    
     let indexPath = collection_view.indexPath(for: item)
    
     print(" Section :\(indexPath?.section)")
     print(" Row :\(indexPath?.row)")
     print(" Item :\(indexPath?.item)")
    

    【讨论】:

    • 显然是 UICollection 中的 row ==item,所以这个答案是不正确的。
    【解决方案3】:
    let collectionIndexPath = textfield.superview?.superview as! UICollectionViewCell
    print(collectionIndexPath.row)
    print(collectionIndexPath.section)
    

    【讨论】:

      最近更新 更多