如何从不同的细胞中获取价值?

问题描述 投票:-2回答:1

我有一个包含多个原型单元格的表视图。我想在这些单元格中获取textField的值,并在点击按钮时追加数组。

Cells有一个叫做element的对象。我想在视图控制器中将此元素追加到elementsArray。问题是当cellForRow方法工作时,这个元素键被创建,但由于cellForRowAt方法只工作一次,元素对象的“value”键取txtContent的初始值。

在为每个单元格写入文本后,如何获取txtContent文本?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let object = surveyDetailArray.first!.elements[indexPath.row]

switch object.type {
case CellConfig.email.rawValue:

        let cell = tableView.dequeueReusableCell(withIdentifier: "EmailCell", for: indexPath) as! EmailCell
        cell.lblTitle.text = object.title
        cell.txtContent.inputAccessoryView = toolBar

        cell.element["formElementId"] = object.id as AnyObject
        cell.element["options"] = optionArray as AnyObject

        elementsArray.append(cell.element as AnyObject)

        return cell

    case CellConfig.number.rawValue:

        let cell = tableView.dequeueReusableCell(withIdentifier: "NumberCell", for: indexPath) as!  NumberCell
        cell.lblTitle.text = object.title
        cell.txtContent.inputAccessoryView = toolBar
        cell.txtContent.keyboardType = .numberPad


        cell.element["formElementId"] = object.id as AnyObject
        cell.element["options"] = optionArray as AnyObject

        elementsArray.append(cell.element as AnyObject)

        return cell
 }

我的细胞类

 class EmailCell: UITableViewCell,UITextFieldDelegate {

@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var txtContent: CustomTextField!

var element = ["formElementId":"",
"value":"",
"options":[]] as [String : Any]

override func awakeFromNib() {
    super.awakeFromNib()
    txtContent.backgroundColor = #colorLiteral(red: 0.1570051014, green: 0.1588717997, blue: 0.2081049681, alpha: 0.1)
    txtContent.layer.borderWidth = 0
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    txtContent.delegate = self
    // Configure the view for the selected state
}

func textFieldDidEndEditing(_ textField: UITextField) {
    element["value"] = textField.text!
}

}
ios swift uitableview
1个回答
0
投票
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let object = surveyDetailArray.first!.elements[indexPath.row]

        switch object.type {
        case CellConfig.email.rawValue:

                let cell = tableView.dequeueReusableCell(withIdentifier: "EmailCell", for: indexPath) as! EmailCell
                cell.txtContent.delegate = self

                return cell

        case CellConfig.number.rawValue:

        let cell = tableView.dequeueReusableCell(withIdentifier: "NumberCell", for: indexPath) as!  NumberCell
return cell
         }


        extension ViewController: UITextFieldDelegate{
        func textFieldDidEndEditing(_ textField: UITextField) {
        var cell: UITableView Cell!
           if let emailCell: UITableViewCell = textField.superview.superview as? EmailCell{
        cell = emailCell
        }

           if let numbCell: UITableViewCell = textField.superview.superview as? NumberCell{
        cell = numbCell
        }

            var table: UITableView = cell.superview as UITableView
            let textFieldIndexPath = table.indexPathForCell(cell)

    // HERE DO THE UPDATION YOU WANT TO DO
        }
        }
© www.soinside.com 2019 - 2024. All rights reserved.