Reputation: 1910
As shown in below image,i created a prototype cell with label and textfield in tableviewController.
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.
Upvotes: 3
Views: 3204
Reputation: 5776
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 "];
}
Upvotes: 3