3

As shown in below image,i created a prototype cell with label and textfield in tableviewController.

enter image description here

Each row is created at runtime as shown below. When user clicks SAVE button all the details like Course detail,Registration id, Username and passowrd should be saved in respective varaibles. But it does not work. It stores value of last textfield in all variables.

How do i get text value in each row of the table.

// Row creation code

cell variable is global
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


        switch(indexPath.row)
        {
            case 0 :
                 cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! RegCell
         cell.configure("Course detail", txtValue: "Enter course.")
                 break;

            case 1 :
                cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! RegCell
                cell.configure("Registration ID", txtValue: "Enter registration id.")
                cell.txtIpValue.tag = 1
                break;

            case 2 :
                cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! RegCell
                cell.configure("Username ", txtValue: "Username")
                cell.txtIpValue.tag = 2
                break;

            case 3 :
                cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! RegCell
                cell.configure("Password ", txtValue: "Password")
                cell.txtIpValue.tag = 2
                break;

            default :
                break;

        }


        return cell
    }

// Save button code

func saveButtonClicked() {

        if(cell.txtIpValue.tag == 1)
        {
            strCourse = cell.txtIpValue.text
        }

    if(cell.txtIpValue.tag == 2)
        {
            strID = cell.txtIpValue.text
        }

    if(cell.txtIpValue.tag == 3)
        {
            strUsername = cell.txtIpValue.text
        }

    if(cell.txtIpValue.tag == 1)
        {
            strPassword = cell.txtIpValue.text
        }      

}

// Regcell

class RegCell: UITableViewCell {

    @IBOutlet var txtIpValue: UITextField
    @IBOutlet var lblstr: UILabel

    func configure(lblValue : String,txtValue :String)) {

        lblstr.text = lblValue
        txtIpValue.text = txtValue
    } 

}

All code is in swift. Please if possible give an example.

sia
  • 1,872
  • 1
  • 23
  • 54

1 Answers1

3

Iterate over all cells in your tableView. Assuming you have only one section, try this:

Update : Using label texts as keys for the values - you might want to think about finding constant values for that

func save() {
    var fields = NSMutableDictionary();
    for rowIndex in 0...self.tableView.numberOfRowsInSection(0) {
        let indexPath : NSIndexPath = NSIndexPath(forItem: rowIndex, inSection: 0);
        let cell : RegCell = self.tableView.cellForRowAtIndexPath(indexPath);
        fields.setObject(cell.txtIpValue.text, forKey: cell.lblstr.text)
    }
    // ... here fields will contain all values, using the lblstr.text as key
    let username = fields["Username "];
}
MarkHim
  • 5,686
  • 5
  • 32
  • 64
  • I suggest you put an identifier property into the RegCell, such as "user_name" or "password" and use them as keys in the mutableDictionary i suggested above. This would separate it from the actual displayed label text. – MarkHim Aug 31 '15 at 15:28
  • very nice answer you saved my day @MarkHim – Nishad Arora Oct 08 '18 at 07:00