获得从表格视图单元格的所有值从文本字段

问题描述 投票:0回答:4

我想从TableViewCell TextFields得到所有值,但我不明白我怎么可以通过点击按钮做。

我可以添加新行,如果我点击按钮Add。所以,我想我可以使尽可能多的行。

我的代码在这里:

struct Section {
    let title: String
    var rows: [String]
}

class SettingsScheduleAndPricesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

    var hall: Halls?

    var sections = [Section]()

    override func viewDidLoad() {
        super.viewDidLoad()
        sections = [Section(title: "Day of week", rows: []),
                    Section(title: "Second section", rows: [""]),
                    Section(title: "Third section", rows: [""])]
    }

    // MARK: - TableView
    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].rows.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            ...
        } else if indexPath.section == 1 {
            let hourlyRateCell = tableView.dequeueReusableCell(withIdentifier: "hourlyRateCell", for: indexPath) as! SettingsHourlyRateCell
            return hourlyRateCell
        } else {
            ...
        }
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section].title
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let frame: CGRect = tableView.frame
        if section == 0 {
            let headerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
            headerView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
            return headerView
        } else if section == 1 {
            let addButton: UIButton = UIButton(frame: CGRect(x: frame.size.width - 50, y: 0, width: 50, height: 30))
            addButton.backgroundColor = UIColor.clear
            addButton.setTitleColor(#colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1), for: .normal)
            addButton.setTitle(NSLocalizableAdd, for: .normal)
            addButton.addTarget(self, action: #selector(SettingsScheduleAndPricesViewController.addHourlyRate(sender:)), for: .touchUpInside)
            let headerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
            headerView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
            headerView.addSubview(addButton)
            return headerView
        } else {
            ...
        }
    }

    @IBAction func saveBarButtonPressed(_ sender: Any) {

        // here I want to get all values from my Cell from my TextFields

    }

    @objc func addHourlyRate(sender: UIButton) {
        let newRow = ""
        append(row: newRow, in: 1)
    }

    func append(row : String, in section: Int) {
        let insertionIndex = sections[section].rows.count
        sections[section].rows.append(row)
        let indexPath = IndexPath(row: insertionIndex, section: section)
        tableView.insertRows(at: [indexPath], with: .automatic)
    }
}

class SettingsHourlyRateCell: UITableViewCell, UITextFieldDelegate {
    @IBOutlet weak var rubHourTF: UITextField!
}

从模拟器我的例子:

enter image description here

在我的例子我需要从三排得firstsecondthird字符串文本。而在空array追加或只是console打印。

在方法@IBAction func saveBarButtonPressed(_ sender: Any)

我找不到任何可以帮助我。

ios swift uitableview uitextfield
4个回答
0
投票

在TableView中细胞的TextField可能会非常棘手。首先,使用的数据模型和reloadData(),而不是insertRows(在:)这样的:

@objc func addHourlyRate(sender: UIButton) {
    let newRow: String = ""
    secondRowText.append(newRow)
    self.tableView.reloadData()
}

现在,设置在单元格中的文本,并在cellForRowAt()这样的行号标记的的UITextField:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let hourlyRateCell = tableView.dequeueReusableCell(withIdentifier: "hourlyRateCell", for: indexPath) as! SettingsHourlyRateCell
   hourlyRateCell.rubHourTF.text = secondRowText[indexPath.row]
   hourlyRateCell.rubHourTF.tag = indexPath.row              
   return hourlyRateCell

}

接下来,使用UITextFieldDelegate保持在文本字段中的任何改变,像跟踪:

func textFieldDidEndEditing(_ textField: UITextField) {
     let tableRow = textField.tag
     secondRowText[tableRow] = textField.text
} 

请注意,在cellForRow()设置的文本字段标签现在是如何用于了解文本字段是在表中哪一行。


0
投票

你可以做一个委托协议,为您的电池

protocol cellDelegate {
func buttonClicked(textFieldText: String)}

使您的协议的一个变量,你的

var delegate: cellDelegate?

然后按一下按钮事件调用从变量的方法

delegate?.buttonClicked(textFieldText: "string you got from textField")

并在表视图cellForRowAt indexPath方法这样设置电池的代表:cell.delegate = self


0
投票

也许我的方法帮助别人,但我仍设法做这种方式。我只添加我button这样的代码:

@IBAction func saveBarButtonPressed(_ sender: Any) {
    let countRowsOne = tableView.numberOfRows(inSection: 1)
        for index in 0...countRowsOne {
            let indexPath = IndexPath(row: index, section: 1)
            if let cell = tableView.cellForRow(at: indexPath) as? SettingsHourlyRateCell {
                ...
            }
            ...
        }
    }
}

-1
投票

无需标签

@IBAction func saveBarButtonPressed(_ sender: Any) {
    var array = NSMutableArray()
    for subview in self.tableView.subViews {
         if subView.isKind(of: String(describing: SettingsHourlyRateCell.self)) {
            for sView in subview {
               if sView.isKind(of: String(describing: UITextField.self)) {   
                 array.add(sView.text)
            }
          }
       }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.