Как получить значения из разных ячеек? - PullRequest
0 голосов
/ 12 февраля 2019

У меня есть табличное представление, которое включает несколько ячеек-прототипов.Я хочу получить значения textField в этих ячейках и добавить массив при нажатии кнопки.

В ячейках есть объект, который называется элемент.Я хочу добавить этот элемент к 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!
}

}

1 Ответ

0 голосов
/ 12 февраля 2019
        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
        }
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...