首页 > 解决方案 > 我可以将 tableViewCell 中 TextField 中的字符串存储到数组中,然后再次使用该数组在 Textfield 中考虑其字符串吗?

问题描述

我想存储UITextField.text到一个数组中,然后将字符串提取到带有我之前输入文本的文本字段的单元格中。

我已经尝试将输入的文本存储到数组中,这似乎可行。

goalsCellEntries是数组。

我使用此函数来更改数组中的值,这似乎有效,因为在控制台中数组的值发生了变化!该函数通过“Editing Did End”事件链接到 TextField:

 @IBAction func goalsTextChangeDidEnd(_ sender: UITextField) {

        if let indexPath = self.goalsTableView.indexPathForView(sender){
            goalsCellEntries[indexPath.row].text = sender.text!
            print(goalsCellEntries)
        }
    }

然后我在函数中调用此函数,cellForRowAt以便在回收 Cell 时使用数组字符串的更新版本:

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

        if tableView == scheduleTableView {

                let cell = tableView.dequeueReusableCell(withIdentifier: "scheduleCell", for: indexPath) as! scheduleTableViewCell

                cell.scheduleStartTimeField.text = scheduleCellEntries[indexPath.row].time01

                cell.scheduleEndTimeField.text = scheduleCellEntries[indexPath.row].time02

                cell.scheduleTextField.text = scheduleCellEntries[indexPath.row].text


            if scheduleCellEntries[indexPath.row].timeButtonState == false{
                cell.scheduleDateCheckBox.isSelected = false

            }else{
                cell.scheduleDateCheckBox.isSelected = true

            }

            if scheduleCellEntries[indexPath.row].textButtonState == false{
                cell.scheduleTextCheckBox.isSelected = false

            }else{
                cell.scheduleTextCheckBox.isSelected = true

            }

                return cell



        } else if tableView == goalsTableView {

            let cell = tableView.dequeueReusableCell(withIdentifier: "goalsCell", for: indexPath) as! goalsTableViewCell

            cell.goalsTextField.text = goalsCellEntries[indexPath.row].text
            goalsTextChangeDidEnd(cell.goalsTextField)


            if goalsCellEntries[indexPath.row].buttonState == false{
                cell.goalsCheckBox.isSelected = false

            }else{
                cell.goalsCheckBox.isSelected = true

            }

            if goalsCellEntries[indexPath.row].buttonState == false{
                cell.goalsCheckBox.isSelected = false

            }else{
                cell.goalsCheckBox.isSelected = true

            }



            return cell
        } else if tableView == toDoTableView {

            let cell = tableView.dequeueReusableCell(withIdentifier: "toDoCell", for: indexPath) as!toDoTableViewCell

            cell.toDoTextField.text = toDoCellEntries[indexPath.row].text


            if toDoCellEntries[indexPath.row].buttonState == false{
                cell.toDoCheckBox.isSelected = false

            }else{
                cell.toDoCheckBox.isSelected = true

            }

            if toDoCellEntries[indexPath.row].buttonState == false{
                cell.toDoCheckBox.isSelected = false

            }else{
                cell.toDoCheckBox.isSelected = true

            }

            return cell
        } else /*if tableView == motivationTableView*/ {
            let cell = tableView.dequeueReusableCell(withIdentifier: "motivationCell", for: indexPath) as! motivationTableViewCell
            cell.motivationImage.image = #imageLiteral(resourceName: "placeholderImage")
            cell.motivationTextField.text = "Write your Motivation here!"

            return cell
        }
    }

但是当我在 tableView 中滚动并且单元格被回收时,UITextField.text它又回到了默认状态。

标签: arraysswifttableviewstoring-dataindexpath

解决方案


推荐阅读