Swift 3:如何从不同collectionView单元格中的单个文本字段中获取文本?

时间:2017-04-01 20:05:36

标签: swift swift3 uicollectionview uitextfield uicollectionviewcell

我有一个简单的UITextField,有3个单元格。单元格拥有自己的类,所有3个单元格都使用此类。单元格类只有UITextFields,即单元格大小。我想知道如何能够掌握3个单元import UIKit class FirstCell: BaseCell { var secondController: SecondController? let textField: UITextField = { let tv = UITextField() tv.textColor = .white return tv }() override func setupViews() { super.setupViews() backgroundColor = .lightGray addSubview(textField) textField.frame = CGRect(x: 12, y: 0, width: self.frame.width, height: self.frame.height) } } private let reuseIdentifier = "Cell" class SecondController: UICollectionViewController, UICollectionViewDelegateFlowLayout { override func viewDidLoad() { super.viewDidLoad() self.collectionView?.backgroundColor = .white self.collectionView?.alwaysBounceVertical = true self.collectionView?.register(FirstCell.self, forCellWithReuseIdentifier: reuseIdentifier) self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Print", style: .plain, target: self, action: #selector(handlePrint)) } func handlePrint() { // print here... } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 3 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: view.frame.width, height: 50) } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? FirstCell cell?.secondController = self if indexPath.item == 0 { cell?.textField.placeholder = "First Textfield" } if indexPath.item == 1 { cell?.textField.placeholder = "Second Textfield" } if indexPath.item == 2 { cell?.textField.placeholder = "ThirdTextfield" } return cell! } } 中每个单元格中输入的内容并将其传递给我的SecondController。我该如何实现这一目标?感谢你们。代码如下:

Eg:n=3,k=2,arr={0,0,0},index=0

1 个答案:

答案 0 :(得分:0)

创建协议

protocol MyCellTextFieldDelegate {
    func didChangeTextTo(text: String)
}

然后

extension SecondController: MyCellTextFieldDelegate {
    func didChangeTextTo(text: String) {
        //doStuff
    }
}

在你的小区里面改变

var secondController: SecondController?

weak var delegate: MyCellTextFieldDelegate?

并将您的方法更改为

override func setupViews() {
    super.setupViews()
    backgroundColor = .lightGray
    addSubview(textField)
    textField.delegate = self //this line
    textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged) //and this line
    textField.frame = CGRect(x: 12, y: 0, width: self.frame.width, height: self.frame.height)
}

和你的细胞类

class FirstCell: BaseCell, UITextFieldDelegate {
    //other code

    private func textFieldDidChange() {
        self.delegate?.didChangeTextTo(text: (self.textField.text ?? ""))
    }
}

cellForItemAt

cell?.delegate = self

编辑:如果您希望调用此UICollectionViewCell,则应添加

var tag: Int = 0

在您的单元格内部和cellForItemAt

cell?.tag = indexPath.row

然后更改您的MyCellTextFieldDelegate功能,不仅要传递text,还要传递tag