Save textField value to cell textLabel (name)

I’m going to do something like this http://img.ioscake.com/ios/jAGsk.png

So if user input points – it’ll save points to the user’s name. How to do it? I paste textField in the tableViewCell with a functions.
Here is code from the tableViewCell file

  • lateral cell space tableview Swift
  • How to input currency format on a text field (from right to left) using Swift?
  • iOS project compilation
  • Random Error with Exit Code 1 - XCODE 6.1 Swift
  • Move view when so that keyboard does not hide text field
  • Finding the rootViewController in iOS
  • @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
    }
    

    And here is code from the VC file

    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
    }
    

    So how to save it to the user’s name?

    3 Solutions Collect From Internet About “Save textField value to cell textLabel (name)”

    You have to create a data model for your users:

    class User: NSObject {
        var points = 0
    }
    

    And then create an array of users in your view controller:

    var users = [User]()
    

    That way, you can do something like this

    var user = users[indexPath.row]
    user.points = 100
    print(user.points) // 100
    

    You can then display your users’ points in your table view. You can also assign a tag to your text fields equal to the indexPath.row so that you can easily work with them.

    In top of use user model provided by @Cesare we need to modified the cellForRowAtIndexPath method and your cell’s implementation, adding a closure for data change event, and using it

    @IBOutlet weak var inputScore: UITextField!
    fileprivate var fnDataWasUpdated : (Int?) -> Void = {_ in} //closure for data change notification
    
    public func configure(text: Int?, placeholder: String,_ fnListener: @escaping (Int?) -> Void) {
        inputScore.text = String(text!)
        inputScore.placeholder = placeholder
    
        inputScore.accessibilityValue = String(text!)
        inputScore.accessibilityLabel = placeholder
    
        //added delegate implementation for UITextField
        inputScore.delegate = self
        self.fnDataWasUpdated = fnListener
    }
    

    also is needed that your cell adopts UITextFieldDelegate protocol

    extension InputScoreTableViewCell : UITextFieldDelegate
    {
        func textFieldDidEndEditing(_ textField: UITextField)
        {
            if let intValue = Int(textField.text)
             {
                 self.fnDataWasUpdated(intValue)
             }
        }
    }
    

    Finally we use the new closure in your cell

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "InputScore") as! InputScoreTableViewCell
        let currUser = self.users[indexPath.row]
        cell.configure(text: currUser.points, placeholder: "Score",{ (newIntValue) in
                    currUser.points = newIntValue
                })
        return cell
    }
    

    This code was not tested but I had been using the main concept in several projects, so if you have any kind of problems please let me know

    I hope this helps you

    Use DidSelectRowAtIndexPath method to get cell textLable text in textField.

    Below Sample Code for That:

    import UIKit
    
    class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
        @IBOutlet var btnOK: UIButton!
        @IBOutlet var txtValue: UITextField!
        @IBOutlet var tblData: UITableView!
        let arrResult = NSMutableArray()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
    
            tblData.dataSource = self
            tblData.delegate = self
            btnOK.tag = 57775
            btnOK.addTarget(self, action: #selector(applyEdit(sender:)), for: .touchUpInside)
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return arrResult.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
            cell.textLabel?.text = arrResult[indexPath.row] as? String ?? ""
            return cell
        }
    
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            btnOK.tag = indexPath.row
            let cell: UITableViewCell = tableView.cellForRow(at: indexPath)!
            txtValue.text = cell.textLabel?.text
            setTitle()
        }
    
        func setTitle() {
            if btnOK.tag == 57775 {
                btnOK.setTitle("Add", for: .normal)
            }else{
                btnOK.setTitle("Update", for: .normal)
            }
        }
    
        func applyEdit(sender: UIButton) {
            if sender.tag == 57775 {
                arrResult.add(txtValue.text ?? "")
            }else{
                arrResult.removeObject(at: sender.tag)
                arrResult.insert(txtValue.text ?? "", at: sender.tag)
                sender.tag = 57775
                setTitle()
            }
            txtValue.text = ""
            tblData.reloadData()
        }
    
    
    }
    

    output:

    enter image description here