从 TextFields 获取 tableViewCell 的所有值
Get all values from tableViewCell from TextFields
我想使用 TextFields
从 TableViewCell
获取所有 值 ,但我不明白如何通过单击按钮来实现。
如果单击按钮 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!
}
我的模拟器示例:
在我的示例中,我需要从三行中获取 first
、second
和 third
字符串文本。并追加空 array
或仅打印 console
.
方法@IBAction func saveBarButtonPressed(_ sender: Any)
.
我找不到任何可以帮助我的东西。
您可以为您的单元制定委托协议
protocol cellDelegate {
func buttonClicked(textFieldText: String)}
并在您的单元格中创建协议变量
var delegate: cellDelegate?
然后在按钮单击事件上从变量调用方法
delegate?.buttonClicked(textFieldText: "string you got from textField")
并在您的 table 查看 cellForRowAt indexPath 方法中像这样设置单元格的委托:cell.delegate = self
TableView 单元格中的文本字段可能很棘手。首先,使用数据模型和 reloadData() 而不是像这样的 insertRows(at:):
@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() 中设置的 textField 标签现在如何用于了解 textField 在 table.
中的哪一行
不需要标签
@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)
}
}
}
}
}
也许我的方法对某些人有帮助,但我还是做到了。我只是在我的 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 {
...
}
...
}
}
}
我想使用 TextFields
从 TableViewCell
获取所有 值 ,但我不明白如何通过单击按钮来实现。
如果单击按钮 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!
}
我的模拟器示例:
在我的示例中,我需要从三行中获取 first
、second
和 third
字符串文本。并追加空 array
或仅打印 console
.
方法@IBAction func saveBarButtonPressed(_ sender: Any)
.
我找不到任何可以帮助我的东西。
您可以为您的单元制定委托协议
protocol cellDelegate {
func buttonClicked(textFieldText: String)}
并在您的单元格中创建协议变量
var delegate: cellDelegate?
然后在按钮单击事件上从变量调用方法
delegate?.buttonClicked(textFieldText: "string you got from textField")
并在您的 table 查看 cellForRowAt indexPath 方法中像这样设置单元格的委托:cell.delegate = self
TableView 单元格中的文本字段可能很棘手。首先,使用数据模型和 reloadData() 而不是像这样的 insertRows(at:):
@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() 中设置的 textField 标签现在如何用于了解 textField 在 table.
中的哪一行不需要标签
@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)
}
}
}
}
}
也许我的方法对某些人有帮助,但我还是做到了。我只是在我的 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 {
...
}
...
}
}
}