iCode
iCode

Reputation: 1466

UITextField placeholder issue

In my app you can select one of the two rows in the first section (section 0). When you select one the rows the row in the section 2 (section 1) reloads. When you change now the selection several times, the placeholder doesn't disappears. (when you change the selection for 1 or 2 times the placeholder disappears). What am I doing wrong?

Screenshot

Here's how I create the UITextField in the cell:

if (!self.selectedType) {
            NSString *string = [NSString stringWithFormat:@"%@: ", self.LessonToDisplay];
            int where = (string.length*5)+35;

            self.textField = [[UITextField alloc]initWithFrame:CGRectMake(where, 10, 150, 25)];
            self.textField.tag = 42;
            self.textField.font = [UIFont fontWithName:@"Helvetica-Neue" size:16];
            self.textField.textColor = [UIColor blackColor];
            if([self.textField.text isEqualToString:@""]){
                self.textField.placeholder = @"Beschreibung";
            }
            [cell.contentView addSubview:self.textField];
            cell.textLabel.text = [NSString stringWithFormat:@"%@: ", self.LessonToDisplay];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            } else {
                NSString *string = [NSString stringWithFormat:@"%@: Prüfung", self.LessonToDisplay];
            int where = (string.length*5.8)+57;
            self.textField2 = [[UITextField alloc]initWithFrame:CGRectMake(where, 10, 150, 25)];
            self.textField2.tag = 43;
            self.textField2.font = [UIFont fontWithName:@"Helvetica-Neue" size:16];
            self.textField2.textColor = [UIColor blackColor];
                if([self.textField2.text isEqualToString:@""]){
                    self.textField2.placeholder = @"Beschreibung";
                }
            [cell.contentView addSubview:self.textField2];

            cell.textLabel.text = [NSString stringWithFormat:@"%@: Prüfung ", self.LessonToDisplay];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }

Upvotes: 1

Views: 296

Answers (1)

Corey
Corey

Reputation: 5818

Going off of @rmaddy's comment, the cells are reused. So, adding a subview to a cells content view will just stack from the previous use. You could just remove all subviews from the cells contentView before adding additional subviews:

for (UIView *subview in cell.contentView.subviews) {
    [subview removeFromSuperview];
}

Upvotes: 2

Related Questions