Answer the question
In order to leave comments, you need to log in
How to get text from textField if it belongs to tableViewCell?
I have a file that is connected to VC, this VC has a tableView. The tableView displays the player's name and points in a cell.
It looks like this
. And I need it to save all the inputs to an array when I click on the finish.
Here is my code with VC
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "InputScore") as! InputScoreTableViewCell
cell.textLabel?.text = usersIn[indexPath.row]
cell.configure(text: 100, placeholder: "Score")
return cell
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return usersIn.count
}
@IBOutlet weak var inputScore: UITextField!
public func configure(text: Int?, placeholder: String) {
inputScore.text = String(text!)
inputScore.placeholder = placeholder
inputScore.accessibilityValue = String(text!)
inputScore.accessibilityLabel = placeholder
}
Answer the question
In order to leave comments, you need to log in
save changes to the text in some temporary collection and when you click on finish, commit the changes.
That is, cell must broadcast these changes to your VC, which writes them to this collection, say Dictionary with the key Cell Index, and the value is text. When you click on finish, iterate through the cell data array and find the text in your Dictionary by index.
var inputScoreArr: [String] = []
func finish() {
for row in 0...usersIn.count {
let indexPath = IndexPath(row: row, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! InputScoreTableViewCell
inputScoreArr.append(cell.inputScore.text)
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question