如何以编程方式为我的 collectionView UITextFields 提供标签?

How do I provide tags for my collectionView UITextFields programmatically?

提前感谢您的阅读。这里是新手。

我有一个collectionView。在其中,我使用自定义 collectionViewCell 来重复我的自定义 cell。每个单元格由 2 UILabels 和 1 UITextfield 组成。

我想在所有 textfields 中输入信息,单击我的计算后 button 我想记录数据。我正在尝试使用 cellForItemAtIndexPath.

分配标签

我的代码如下

var inputNamesArray = ["Ice Cream", "Number of Scoops", "Main Dish", "Side Dish"]

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as? inputCell else {
        fatalError()
    }

    let inputLabels = inputNamesArray[indexPath.row]
    let inputUnits = unitArray[indexPath.row]

    cell.inputLabel.text = inputLabels
    cell.inputText.tag = inputLabels.count //this isn't working
    cell.inputUnitLabel.text = inputUnits  
   // cell.inputText.allowsEditingTextAttributes = true
    return cell
}

inputLabel inputTextinputUnitLabel 是标签和文本字段的名称。

有人知道如何在 cellForItemAtIndexPath 方法中为我的 uitextfield 添加标签吗?

我还阅读了可以使用 enum 的 Apple 文档,但我真的不明白如何使用。

任何帮助,使用任何方法 give tags to my (reusable cell) UITextfields so that I can store and use the data 都会很有帮助!非常感谢!!

p.s。我知道我上面的代码不起作用的原因是因为我正在使用

 override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print(indexPath.count)
}

它打印出标签的标签,但当我点击文本字段时没有标签。我也试过删除

cell.inputText.tag = inputLabels.count

但它仍然 return textfields 的任何标签。

p.s.2 如果你有兴趣,这是我计算按钮的代码,如果有什么我可以在那里做的,请告诉我:)

   @objc func calculateButtonActions() {
    print("calculate tap worked")
    let layout = UICollectionViewFlowLayout()
    let resultsPage = ResultsPageController(collectionViewLayout: layout)
    self.navigationController?.pushViewController(resultsPage, animated: true)
}

和 ViewDidLoad() 中的这个声明

ViewDidLoad() {
let button = UIButton(frame: CGRect(x: 0, y: 550, width:     view.frame.width, height: 100))
    button.setTitle("Calculate", for: .normal)
    button.setTitleColor(.white, for: .normal)
    button.backgroundColor = UIColor.gray

    button.addTarget(self, action: #selector(calculateButtonActions), for: .touchUpInside)
    self.view.addSubview(button)

}

再次感谢!

cellForItemAt委托集你cell.inputText.delegate = self

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell : CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell // REPLACE YOUR CELL NAME WITH CollectionViewCell

        cell.inputText.delegate = self
        return cell
    }

现在您可以使用如下所示的 textViewDidBeginEditing 委托来获取点击的文本视图数据。

extension ViewController: UITextViewDelegate { // REPLACE ViewController with Your ViewController Name
    func textViewDidBeginEditing(_ textView: UITextView) {
        var cell: CollectionViewCell?
        cell = textView.superview?.superview as? CollectionViewCell
        print(cell?.txtVW.text as! String)
    }
}