1

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

截屏

这是我在单元格中创建 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;
            }
4

1 回答 1

2

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

for (UIView *subview in cell.contentView.subviews) {
    [subview removeFromSuperview];
}
于 2013-11-14T20:52:11.380 回答