简体   繁体   English

UITextField 占位符在点击时一直显示

[英]UITextField placeholder keeps showing when tapped

I have a table view with 3 rows.我有一个 3 行的表格视图。 Each row has the same custom tableviewcell with a uitextfield but the placeholder property differs.每行都有相同的自定义 tableviewcell 和 uitextfield 但占位符属性不同。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"NewDestinationCell";

    NewDestinationCell *cell = (NewDestinationCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        [cellNib instantiateWithOwner:self options:nil];
        cell = editCell;
        self.editCell = nil;
        cell.detailTextField.delegate = self;
        cell.detailTextField.tag = indexPath.row;
        [cell.detailTextField setInputAccessoryView:keybdToolbar];
    }
    if ([[textFieldStrings objectAtIndex:indexPath.row] length] != 0) 
        cell.detailTextField.text = [textFieldStrings objectAtIndex:indexPath.row];
    return cell;
}

When I tap the text field and enter some text, go to another text field and then go back to the text field with my text entered, it erases what I typed and puts the placeholder text instead.当我点击文本字段并输入一些文本时,go 到另一个文本字段,然后 go 返回文本字段并输入我的文本,它会删除我输入的内容并放置占位符文本。 Do I have to implement the commitEditingStyle method?我必须实现 commitEditingStyle 方法吗? The text field in each table row is linked to the same uitextfield outlet.每个表格行中的文本字段链接到同一个 uitextfield 插座。 Maybe that's why?也许这就是为什么? Here is the code I'm using to traverse through the three rows.这是我用来遍历三行的代码。

- (void)textFieldDidBeginEditing:(UITextField *)textField {
        [textField becomeFirstResponder];
    textField.text = [textFieldStrings objectAtIndex:textField.tag];
    currTextField = textField;
    if (currTextField.tag == [self.tableView numberOfRowsInSection:0]-1) {
        NextButton.enabled = NO;
        PrevButton.enabled = YES;
    }
    if (currTextField.tag == 0) {
        PrevButton.enabled = NO;
        NextButton.enabled = YES;
    }

}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [textFieldStrings replaceObjectAtIndex:textField.tag withObject:textField.text];
    [textField resignFirstResponder];
}


- (IBAction)PrevTextField:(id)sender {
    [tableViewCellFields[currTextField.tag-1] becomeFirstResponder];
}

- (IBAction)NextTextField:(id)sender {
    [tableViewCellFields[currTextField.tag+1] becomeFirstResponder];
}

Truth be told, that IBOutlet is a bit confusing.说实话, IBOutlet有点令人困惑。 But there are some issues here one of which is cell reuse.但是这里有一些问题,其中之一是单元重用。

Since the cells are reused, you shouldn't rely on text retaining their values and rather you should store what the typed in textFieldDidEndEditing: method.由于单元格被重复使用,因此您不应依赖保留其值的文本,而应存储在textFieldDidEndEditing:方法中键入的内容。 Maintain an array for the values entered or not entered (using [NSNull null] singleton).为输入或未输入的值维护一个数组(使用[NSNull null]单例)。 In cellForRowAtIndexPath: method, if you see an existing text value set the text field's text to that value.cellForRowAtIndexPath:方法中,如果您看到现有文本值,请将文本字段的文本设置为该值。 This way you can counter the cell reuse effect.这样你就可以对抗细胞重用效应。

Another issue is with the outlet StreetName .另一个问题是插座StreetName When the cells are created, I am guessing StreetName will point to the correct text field but what happens when the cells are reused.创建单元格时,我猜StreetName将指向正确的文本字段,但是当单元格被重用时会发生什么。 StreetName would be pointing to the text field of the last cell that was created and as a result all assignments you do in cellForRowAtIndexPath: are incorrect for reused cells. StreetName将指向创建的最后一个单元格的文本字段,因此您在cellForRowAtIndexPath:中所做的所有分配对于重用的单元格都是不正确的。 It would be a lot easier if you create a custom subclass of UITableViewCell where you will do cell.myTextField.text = [textFieldStrings objectAtIndex:indexPath.row];如果您创建UITableViewCell的自定义子类,您将在其中执行cell.myTextField.text = [textFieldStrings objectAtIndex:indexPath.row];会容易得多. .

As a side note,作为旁注,

StreetName.delegate = self;
StreetName.tag = indexPath.row;
tableViewCellFields[indexPath.row] = StreetName;
[StreetName setInputAccessoryView:keybdToolbar];

The first and last line are something you only need to do once while creating the cell.第一行和最后一行是您在创建单元格时只需要执行一次的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM