Normally I have labels and some text fields which are IBOutlets and if someone clicks a button, I can read the text values of these textfields easily with
self.myTextfield1.text;
But now I have a UITableView with customized table cells. In these cells I have a label and a textfield. On load, I do this in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell*)[theTableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (!cell) {
[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
cell = self.myCell;
self.myCell = nil;
}
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
cell.myCellLabel.text = @"Label1";
cell.myCellTextField.text = @"...";
break;
case 1:
cell.myCellLabel.text = @"Label2";
cell.myCellTextField.text = @"...";
break;
// and so on...
default:
break;
}
break;
case 1:
switch (indexPath.row) {
case 0:
cell.myCellLabel.text = @"Label1_1";
cell.myCellTextField.text = @"...";
break;
// and so on...
}
default:
break;
}
return cell;
}
So If someone clicks the button, I cannot access the text values of the fields with self.mytextfield1.text. How can I get the value of the fields? I know in which section and which row which value for a label should be. So if I can get the value of the text field in combination of the section and row number this would be great.
Perhaps anyone has an idea how to solve this problem?
Best Regards, Tim.
If you store the values of the textfields in an array then the index of the array would be the same as the row. You could even have multiple arrays:
cellLabelArraycellTextFieldArraywhich isn’t terribly elegant but would solve the problem. Then you would get rid of your switch statement and do something like this:
Using an array to be the datasource of a UITableView is a common pattern.
I don’t know what happens when your button gets pressed, but if you need to change the values of the labels, you would just change the values in the array and then send the tableview a
reloadDatamessage.