G
G
GOODDUDE2017-07-07 21:05:33
Swift
GOODDUDE, 2017-07-07 21:05:33

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 4b3d4d8cf6a7428ab6586f052712dfc2.png
. 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
    }

Here is the code with tableViewCell
@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

2 answer(s)
I
Igor Cherny, 2017-07-08
@freeg0r

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.

A
Anton Gorb, 2017-07-09
@AntonGorb

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 question

Ask a Question

731 491 924 answers to any question