Reputation: 692
I'm encountering an issue where duplicate UITextField's are being added when the cells are re-used, which isn't what I want to happen. I recall encountering a similar issue previously, but for the life of me cannot remember what I did to solve it.
It's no doubt something obvious, but I can't seem to find anything helpful. I've tried encasing the if/else statements in an 'if (cell == null)' as some people have been suggested, but this just results in a blank table being formed.
Some advice would be greatly appreciated.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (indexPath.section == 0){
productField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[productField setPlaceholder:@"Product"];
if ([productData length] > 0){
[productField setText:productData];
[productField setEnabled:false];
}
[cell addSubview:productField];
} else if (indexPath.section == 1){
issueField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[issueField setPlaceholder:@"What's the issue?"];
[issueField setAutocorrectionType:UITextAutocorrectionTypeNo];
[cell addSubview:issueField];
} else if (indexPath.section == 2){
emailField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[emailField setPlaceholder:@"Email address"];
[emailField setAutocorrectionType:UITextAutocorrectionTypeNo];
[emailField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[emailField setKeyboardType:UIKeyboardTypeEmailAddress];
[cell addSubview:emailField];
} else if (indexPath.section == 3){
notesField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[notesField setPlaceholder:@"Any notes to add?"];
[notesField setAutocorrectionType:UITextAutocorrectionTypeNo];
[cell addSubview:notesField];
} else if (indexPath.section == 4){
sendFeedback = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[sendFeedback setTitle:@"Send Feedback" forState:UIControlStateNormal];
[sendFeedback setBackgroundColor:[UIColor colorWithRed:(111/255.0f) green:(31/255.0f) blue:(68/255.0f) alpha:1.0f]];
[sendFeedback.titleLabel setFont:[UIFont fontWithName:@"MaryAnn" size:20.0]];
[sendFeedback addTarget:self action:@selector(sendFeedback:) forControlEvents:UIControlEventTouchUpInside];
[cell setBackgroundColor:[UIColor clearColor]];
[cell addSubview:sendFeedback];
}
return cell;
}
Upvotes: 1
Views: 460
Reputation: 3656
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
if (indexPath.section == 0)
{
productField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[productField setPlaceholder:@"Product"];
if ([productData length] > 0){
[productField setText:productData];
[productField setEnabled:false];
}
[cell addSubview:productField];
}
else if (indexPath.section == 1)
{
issueField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[issueField setPlaceholder:@"What's the issue?"];
[issueField setAutocorrectionType:UITextAutocorrectionTypeNo];
[cell addSubview:issueField];
} else if (indexPath.section == 2){
emailField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[emailField setPlaceholder:@"Email address"];
[emailField setAutocorrectionType:UITextAutocorrectionTypeNo];
[emailField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[emailField setKeyboardType:UIKeyboardTypeEmailAddress];
[cell addSubview:emailField];
} else if (indexPath.section == 3){
notesField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[notesField setPlaceholder:@"Any notes to add?"];
[notesField setAutocorrectionType:UITextAutocorrectionTypeNo];
[cell addSubview:notesField];
} else if (indexPath.section == 4){
sendFeedback = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[sendFeedback setTitle:@"Send Feedback" forState:UIControlStateNormal];
[sendFeedback setBackgroundColor:[UIColor colorWithRed:(111/255.0f) green:(31/255.0f) blue:(68/255.0f) alpha:1.0f]];
[sendFeedback.titleLabel setFont:[UIFont fontWithName:@"MaryAnn" size:20.0]];
[sendFeedback addTarget:self action:@selector(sendFeedback:) forControlEvents:UIControlEventTouchUpInside];
[cell setBackgroundColor:[UIColor clearColor]];
[cell addSubview:sendFeedback];
}
}
return cell;
}
Upvotes: 0
Reputation: 4901
your problem is reusable cells in table view, try this code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
textField.tag=101;
[cell addSubview:textField];
UIButton *sendFeedback = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
sendFeedback.tag=102;
[sendFeedback setTitle:@"Send Feedback" forState:UIControlStateNormal];
[sendFeedback setBackgroundColor:[UIColor colorWithRed:(111/255.0f) green:(31/255.0f) blue:(68/255.0f) alpha:1.0f]];
[sendFeedback.titleLabel setFont:[UIFont fontWithName:@"MaryAnn" size:20.0]];
[sendFeedback addTarget:self action:@selector(sendFeedback:) forControlEvents:UIControlEventTouchUpInside];
[cell setBackgroundColor:[UIColor clearColor]];
[cell addSubview:sendFeedback];
}
UITextField* textField=(UITextField*)[cell viewWithTag:101];
UIButton *sendFeedback = (UIButton*)[cell viewWithTag:102];
sendFeedback.hidden=YES;
textField.hidden=NO;
[textField setKeyboardType:UIKeyboardTypeDefault];
if (indexPath.section == 0)
{
[textField setPlaceholder:@"Product"];
if ([productData length] > 0)
{
[textField setText:productData];
[textField setEnabled:false];
}
} else if (indexPath.section == 1){
[textField setPlaceholder:@"What's the issue?"];
[textField setAutocorrectionType:UITextAutocorrectionTypeNo];
} else if (indexPath.section == 2){
[textField setPlaceholder:@"Email address"];
[textField setAutocorrectionType:UITextAutocorrectionTypeNo];
[textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[textField setKeyboardType:UIKeyboardTypeEmailAddress];
} else if (indexPath.section == 3){
[textField setPlaceholder:@"Any notes to add?"];
[textField setAutocorrectionType:UITextAutocorrectionTypeNo];
} else if (indexPath.section == 4)
{
textField.hidden=YES;
sendFeedback.hidden=NO;
}
return cell;
}
// You not directly access textField.text in table view,you assign textField.text using global variable or dictionary
Upvotes: 2
Reputation: 949
Why not after you instantiate the cell, you remove all of the subviews from the cells contentView
for( UIView *view in cell.contentView.subviews )
[view removeFromSuperview];
Upvotes: 0
Reputation: 6707
The problem is that since you're recycling cells (and you should), you are reusing non empty cells and add new controls on top of it.
The usual way to handle that is to build your custom UITableViewCell subclass, and have the various textfields and other UI controls as properties. Then you can show/hide those UI controls in each section
It would look something like that:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (indexPath.section == 0){
productField.hidden=NO;
issueField.hidden=YES;
} else if (indexPath.section == 1){
productField.hidden=YES;
issueField.hidden=NO;
}
return cell;
}
Upvotes: 0
Reputation: 7343
It is happening because you keep adding the subviews to the cells. For example you add the productField
to the cell at index 0. When that cell is reused, you add another subview to it, but the productField
would still be there. A possible solution would be to remove the subview before adding a new one. Your if
statements would be something like this:
if (indexPath.section == 0){
// Remove the old subview
UIView *oldSubview = [cell viewWithTag:5];
[oldSubview removeFromSuperview];
productField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 300, 45)];
[productField setPlaceholder:@"Product"];
if ([productData length] > 0){
[productField setText:productData];
[productField setEnabled:false];
}
// Set a tag for the new subview, so you can remove it later
productField.tag = 5;
[cell addSubview:productField];
}
Use the same tag
in every if
statement.
Edit:
Or you can just do it like @pawan suggested and use static cells.
Upvotes: 0