0

I'm going to do something like this https://i.stack.imgur.com/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

@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?

  • Have a look this solution: https://stackoverflow.com/a/42159435/4488252 – Vasily Bodnarchuk Jul 08 '17 at 09:32
  • @GOODDUDE SO is a community where some people are solving some other people problems only for reputation of helped people as prove of gratitude, please show you're grateful for the time spended in solve your answer – Reinier Melian Jul 17 '17 at 03:00

3 Answers3

1

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

Sakir Sherasiya
  • 1,562
  • 1
  • 17
  • 31
0

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.

Cesare
  • 9,139
  • 16
  • 78
  • 130
0

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

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
  • answering your first comment, did you use closures before?, when the value change the closure is executed and modify the value of the model user of this cell, did you try my code? – Reinier Melian Jul 09 '17 at 10:22