ios - 检索自定义单元格内的 textfield.text

标签 ios swift

我有一个包含自定义单元格的表格 View 。 每个单元格都有 3 个文本字段:dayInWeek、startTime、endTime。 在下图中,它有 2 行。但用户可以单击 + 按钮添加更多行。

如果用户单击“提交”按钮,我想循环到每一行,收集 3 个文本字段数据,并存储在数组或其他内容中。

enter image description here

自定义 TableViewCell:

import UIKit

class RegularScheduleCell: UITableViewCell {
    
    
    @IBOutlet weak var dayInWeek: UITextField!
    @IBOutlet weak var startTime: UITextField!
    @IBOutlet weak var endTime: UITextField!
      
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code


    }
}

还有一个 View Controller :

import UIKit


class RegularScheduleVC: UIViewController, UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    
    
    var numOfRow = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return numOfRow
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "RegularScheduleCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RegularScheduleCell
        
        return cell   
        
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.delete) {
            
           numOfRow -= 1
            self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.right)
           
        tableView.reloadData()
        }
    }
   
    
    func insertNewRow(_ sender: UIBarButtonItem) {
        if numOfRow < 7 {
            numOfRow += 1
            tableView.reloadData()
            
        }
        
       

    }
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        tableView.setEditing(editing, animated: animated)
    }
    
    
}

此时我尝试使用UITextFieldDelegate

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "RegularScheduleCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RegularScheduleCell
        
        cell.dayInWeek.delegate = self
        cell.startTime.delegate = self
        cell.endTime.delegate = self
        return cell
    

func textFieldDidEndEditing(_ textField: UITextField) {
        allCellsText.append(textField.text!) //allCellsText is an array
        print(allCellsText) 
    }

这样当用户完成编辑时,然后将该数据添加到数组中。 但是,这不能满足我的要求,因为:

  • 在同一个单元格上:无法知道数据是属于 dayOfWeek、startTime 还是 endTime

  • 在 2 个不同的单元格上:无法知道数据是属于第一个单元格的星期几还是第二个单元格的星期几。

因此,如何循环到所有单元格,获取所有 3 个文本字段数据? 谢谢

最佳答案

方法一:

制作一个 key 对数组作为-

    var arrayOfKeyPairs = [[String:Any]]()

    arrayOfKeyPairs.append(["header":"xx",
                            "value" : "",
                            “id”: "dsd",
                            "order" : 0])

我们只是将默认值替换为用户输入值作为-

func textFieldDidEndEditing(_ textField: UITextField) {
        let center: CGPoint = textField.center
        let rootViewPoint: CGPoint = textField.superview!.convert(center, to: tableView)
        let indexPath: IndexPath = tableView.indexPathForRow(at: rootViewPoint)! as IndexPath
        arrayOfKeyPairs[indexPath.row ]["value"] = textField.text//here you are appending(replacing) data to array
}

点击提交按钮,交叉检查你收到的是什么-

func tapsOnNext(){
  self.view.endEditing(true)//for adding last text field value with dismiss keyboard
  print(arrayOfKeyPairs)
}

方法二: 我们可以通过访问具有特定 indexpath as

的单元格来获取单元格数据
func tapsOnNext(){
     let indexpath = IndexPath(row: 0, section: 0)
     let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
     print(cell.myTextField.text)
  }

关于ios - 检索自定义单元格内的 textfield.text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45320811/

相关文章:

ios - 当我将它从后台加载到我的主类时,按钮操作抛出异常

swift - 我必须使用协议(protocol)/委托(delegate)来让 ViewController 执行在另一个类中创建的 UIButton 的操作吗?

ios - objc_sync_enter/objc_sync_exit 不适用于 DISPATCH_QUEUE_PRIORITY_LOW

ios - Apple ID 登录循环?

ios - Admob 链接器命令失败

ios - 如何在 Swift (Xcode) 上通过代码显示屏幕?

ios - 函数产生预期类型 "String",你的意思是用 "()"调用它吗

ios - UserDefaults 未在 TextView 中保留数据

ios - 如何配置 Pods 项目的build设置?

ios - 从 iPhone 仅获取新的/修改的电话联系人