Custom Table View Cells with TextField Problems
HI, So here's what I'm trying to do. I have a table View and I'm using Custom Table View Cells with text Fields. Basically you enter Text in the fields and it saves it to a managed object context. I have set up the Table View thusly;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//static NSString *CustCell=@"CustomCell";
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell== nil) {
cell= [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
switch (indexPath.row) {
case 0:
cell.primaryLabel.text = @"Name";
cell.mainTextField.text=product.prodName;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
case 1:
cell.primaryLabel.text = @"Store";
cell.mainTextField.text=product.store;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
case 2:
cell.primaryLabel.text=@"Amount";
cell.mainTextField.text=product.amount;
cell.mainTextField.keyboardType=UIKeyboardTypeNumberPad;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
case 3:
cell.primaryLabel.text=@"Measure";
cell.detailLabel.text=product.measure;
cell.mainTextField.hidden=YES;
break;
case 4:
cell.primaryLabel.text=@"Package";
cell.detailLabel.text=product.container;
cell.mainTextField.hidden=YES;
break;
case 5:
cell.primaryLabel.text=@"Aisle";
cell.detailLabel.text=product.aisle;
cell.mainTextField.hidden=YES;
break;
case 6:
cell.primaryLabel.text=@"Note";
cell.mainTextField.text=product.note;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
default:
break;
}
return cell;
}
So here's my issue. At runtime I can enter text in cells 1-3 no problem it saves and everything's happy. But if I enter anything in cell 6 it returns Null for cell.nameTextField.text and I don't know why.
What I've done. 1. I tried creating a single instance cell in the nib and placing it in that cell. 2. I tried Setting it to index.row 0 of a new section. 3. Tried adding a text field to the content view of a standard cell at that index. 4. commented out the save method for that cell. 5. tried creating a separate textField and setting the cell.mainTextFiled to Hidden. 6. Tried creating the table View Programmatically without using the nib. 7. It all worked when I used single instance cells fro开发者_运维知识库m the nib for all four of these cells. If that's the answer, so be it. But Custom Table View Cells created programmatically are so much more concise and elegant.
To add insult to insanity If I move that cell to an index of 5 or below everything works great. Any index over 5 and it blows up.
I have scoured the web and the dev docs as well as the 4 iPhone Dev books that I have. I am here because I have exhausted what I can do. I'm sure it's something dumb that I'm missing but I'm flummoxed. Any help would be appreciated. The ultimate goal is to have have notes in a second section with more room to write. Yes I know there are other ways to do this but it seems that this should work and I would love to know why it doesn't. Oh, I should mention that I have only been using the iPhone simulator. I have not tried running on a phone. Thanks.
OK, I'm an idiot. After posting this I found the answer (of course!) Simply put. When the cell with the text field scrolls out of view it dequeues the cell. When the cell reappears it gets re-instantiated with a shiny clean text field. So now I'm looking at ways to overcome this. Arrays come to mind. I'll post what I find. If anyone has ideas about this problem I'd love to hear them.
精彩评论