Ios 如何以编程方式为collectionView UITextFields提供标记?

Ios 如何以编程方式为collectionView UITextFields提供标记?,ios,uitextfield,Ios,Uitextfield,提前感谢您的阅读。我是新手 我有一个收藏视图。在其中,我使用一个自定义的collectionViewCell来重复我的自定义单元格。每个单元格由2个UILabels和1个UITextfield组成 我想在所有文本字段中输入信息,然后单击我的计算按钮我想记录数据。我正在尝试使用cellForItemAtIndexPath分配标记 我的代码如下 var inputNamesArray = ["Ice Cream", "Number of Scoops", "Main Dish", "Side Dis

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

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

我想在所有
文本字段中输入信息
,然后单击我的计算
按钮
我想记录数据。我正在尝试使用
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
inputText
inputUnitLabel
是标签和文本字段的名称

有人知道如何在
cellForItemAtIndexPath
方法中为我的
uitextfield
提供标记吗

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

任何帮助,使用任何方法
为我的(可重用单元)UITextfields提供标记,以便我可以存储和使用数据
,都将非常有用!非常感谢

p、 我之所以知道我的上述代码不起作用,是因为我正在使用

 override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print(indexPath.count)
}
它会打印出标签的标签,但当我点击文本字段时没有标签。此外,我还尝试了删除

cell.inputText.tag = inputLabels.count
但是它仍然不会为
文本字段
返回任何标记

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

以及ViewDidLoad()中的此声明

}


再次感谢

cellForItemAt
delegate中设置您
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
delegate获取点击的textview数据,如下所示

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)
    }
}

cell.inputText.tag=inputLabels.count
替换为
cell.inputText.tag=indexPath.row
非常感谢!我正在使用这个新的声明,但是我现在如何调用每个特定的textfield来使用它们的数据呢?因为单元格仅在
cellForItemAtIndexPath
中声明,非常感谢!所以你也希望得到点击的文本字段数据,对吗?是的,我知道如何对我特别命名的文本字段执行此操作,这是我的代码:
let resultadresslabel:UILabel={let label=UILabel()return label}()
然后
var address=resultadresslabel.text
。但在上述(可重复细胞)的情况下,我不知道如何做到这一点。或者具体在哪里申报。再次感谢你的帮助!嗨,库尔德普!我仍然无法检索每个文本字段的值。这是我的代码:
将func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{guard let cell=collectionView.dequeueReusableCell(带有ReuseIdentifier:“cellId”,for:indexPath)重写为?inputCell else{//inputCell是UICollectionViewCell fatalError()}cell.inputText.delegate=(自身为?UITextFieldDelegate)
的子类,我声明
func TextFieldDiEndEditing(textField:UITextField){var cell:inputCell?cell=textField.superview?superview as?inputCell打印(cell?.inputText.text as Any)}
因为我需要在完成所有输入后检索信息(总共有6个单元格。我的问题仍然是我需要以有序的方式检索用户输入的信息(可能是数组?)所以我可以使用这些值进行计算。这是我正在制作的一个非常广泛的计算器。非常感谢,我尝试使用扩展名的方式非常感谢任何帮助,但我得到了一个错误,即
声明仅在文件范围内有效
,所以我想我不能在视图控制器内添加扩展名?@valeriana,他说re是演示链接,其中CollectionView和完整代码中有textView和TextField。它会帮助您。嗨,kuldeep!非常感谢!我尝试打开它,但它只是一个在故事板上带有按钮的文件,没有连接到任何代码:(
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
    }
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)
    }
}