UITextField占位符问题

时间:2013-11-14 20:41:01

标签: ios objective-c uitableview uitextfield

在我的应用程序中,您可以选择第一部分中的两行之一(第0部分)。当您选择一行时,第2部分(第1部分)中的行将重新加载。当您多次更改选择时,占位符不会消失。 (当您更改选择1或2次时,占位符消失)。我做错了什么?

Screenshot

以下是我在单元格中创建UITextField的方法:

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;
            }

1 个答案:

答案 0 :(得分:2)

关于@ rmaddy的评论,细胞被重复使用。因此,将子视图添加到单元格内容视图将只是从以前的使用堆叠。您可以在添加其他子视图之前从单元格contentView中删除所有子视图:

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