gpt4 book ai didi

ios - 将 uitextField 滚动到 View 之外时,占位符重新出现 - ios

转载 作者:行者123 更新时间:2023-12-01 17:56:11 25 4
gpt4 key购买 nike

我有一个 uiview,由顶部的 map View 和下面的 uitableview 组成。表格 View 中有带有 uitextFields 的单元格和占位符。

当我编辑字段时,占位符会正确消失,但是如果我将 uitableviewcells 滚动到视线之外(即位于表格 View 上方的 map View “后面”),占位符会与写入 uitextfield 的内容一起重新出现。

如果我用普通文本替换占位符,并清除 textFieldDidBeginEditing 上的文本,则会发生相同的重影效果。占位符文本和输入都显示。

我尝试在 textFieldDidBeginEditing 中将占位符和文本设置为零,但无济于事。

表格 View 使用 michaeltyson's TPKeyboardAvoidingTableView .

Ghost placeholder

编辑
添加了我的 cellForRowAtIndex:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell%d", indexPath.row ]];
cell.textLabel.text = _tableData[indexPath.row];
if (indexPath.row == 0) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.placeholder = @"(Required)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 1) {
UILabel *textField = [[UILabel alloc] initWithFrame:CGRectMake(110, 6, 185, 30)];
textField.text = @"(Required)";
textField.textColor = [UIColor lightGrayColor];
textField.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:textField];
secondCellField = textField;
secondCell = cell;
} else if (indexPath.row == 2) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.placeholder = @"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 3) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.placeholder = @"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 4) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.placeholder = @"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 5) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.placeholder = @"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 6) {
UISwitch *textField = [[UISwitch alloc] initWithFrame:CGRectMake(210, 8, 50, 30)];
[textField addTarget:self action:@selector(segwayToWork) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:textField];

_workSwitch = textField;
}

return cell;

最佳答案

您的问题是多个 TextFields为不同的单元格创建。您需要使用 reusable细胞
像:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
// allocate the cell:
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.row == 0) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.tag = 1111;//tag
textField.placeholder = @"(Required)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 1) {
UILabel *textField = [[UILabel alloc] initWithFrame:CGRectMake(110, 6, 185, 30)];
textField.text = @"(Required)";
textField.tag = 1111;//tag
textField.textColor = [UIColor lightGrayColor];
textField.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:textField];
secondCellField = textField;
secondCell = cell;
} else if (indexPath.row == 2) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.tag = 1111;//tag
textField.placeholder = @"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 3) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.tag = 1111;//tag
textField.placeholder = @"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 4) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.tag = 1111;//tag
textField.placeholder = @"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 5) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.tag = 1111;//tag
textField.delegate = self;
textField.placeholder = @"(Optional)";
[cell.contentView addSubview:textField];
} elseif (indexPath.row == 6) {
UISwitch *textField = [[UISwitch alloc] initWithFrame:CGRectMake(210, 8, 50, 30)];
textField.tag = 1111;//tag
[textField addTarget:self action:@selector(segwayToWork) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:textField];

_workSwitch = textField;
}

}
cell.textLabel.text = _tableData[indexPath.row];

return cell;

}

我已经尝试重用 UITableViewCell因此,请根据您的要求对其进行自定义。您也可以使用此代码。

关于ios - 将 uitextField 滚动到 View 之外时,占位符重新出现 - ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17272766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com