我在集成UITextfield
为子视图时遇到问题UITableViewCell
。我在我的项目中使用了以下代码UITextField
作为子视图添加UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier=@"CellIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.row==languageName) {
if (langNameTxtFld==nil) {
langNameTxtFld=[[UITextField alloc]initWithFrame:CGRectMake(50,0, 265, 44)];
langNameTxtFld.delegate=self;
[cell addSubview:[self addTextFieldWithText:nil textField:langNameTxtFld placeholder:@"Language Name" returnType:UIReturnKeyNext]];
}
}
else if (indexPath.row==region) {
if (regionTxtFld==nil) {
regionTxtFld=[[UITextField alloc]initWithFrame:CGRectMake(50,0, 265, 44)];
regionTxtFld.delegate=self;
[cell addSubview:[self addTextFieldWithText:nil textField:regionTxtFld placeholder:[globalPlaceHolderDict valueForKeyPath:@"region_meaning"] returnType:UIReturnKeyNext]];
}
}
else if (indexPath.row==city) {
if (cityTxtFld==nil) {
cityTxtFld=[[UITextField alloc]initWithFrame:CGRectMake(50,0, 265, 44)];
cityTxtFld.delegate=self;
[cell addSubview:[self addTextFieldWithText:nil textField:cityTxtFld placeholder:[globalPlaceHolderDict valueForKeyPath:@"city_meaning"] returnType:UIReturnKeyNext]];
}
}
else if (indexPath.row==school) {
if (schoolsTxtFld==nil) {
schoolsTxtFld=[[UITextField alloc]initWithFrame:CGRectMake(50,0, 265, 44)];
schoolsTxtFld.delegate=self;
[cell addSubview:[self addTextFieldWithText:nil textField:schoolsTxtFld placeholder:[globalPlaceHolderDict valueForKeyPath:@"schools_meaning"] returnType:UIReturnKeyNext]];
}
}
else if (indexPath.row==studies){
if (studiesTxtFld==nil) {
studiesTxtFld=[[UITextField alloc]initWithFrame:CGRectMake(50,0, 265, 44)];
studiesTxtFld.delegate=self;
[cell addSubview:[self addTextFieldWithText:nil textField:studiesTxtFld placeholder:[globalPlaceHolderDict valueForKeyPath:@"studies_meaning"] returnType:UIReturnKeyNext]];
}
}
else if (indexPath.row==email) {
if (emailTxtFld==nil) {
emailTxtFld=[[UITextField alloc]initWithFrame:CGRectMake(50,0, 265, 44)];
emailTxtFld.delegate=self;
[cell addSubview:[self addTextFieldWithText:nil textField:emailTxtFld placeholder:[globalPlaceHolderDict valueForKeyPath:@"email"] returnType:UIReturnKeyNext]];
}
}
else if (indexPath.row==passwords){
if (passwordsTxtFld==nil) {
passwordsTxtFld=[[UITextField alloc]initWithFrame:CGRectMake(50,0, 265, 44)];
passwordsTxtFld.delegate=self;
[cell addSubview:[self addTextFieldWithText:nil textField:passwordsTxtFld placeholder:[globalPlaceHolderDict valueForKeyPath:@"passwords"] returnType:UIReturnKeyNext]];
}
}
}
cell.backgroundColor=[UIColor whiteColor];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
为了将 TextField 添加到表格单元格中,我使用了如下自定义函数
-(id)addTextFieldWithText:(NSString*)text textField:(id)txtFld placeholder:(NSString*)placeholder returnType:(UIReturnKeyType)keyType {
[txtFld setTextAlignment:UITextAlignmentLeft];
[txtFld setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[txtFld setTextColor:[UIColor blackColor]];
[txtFld setAutocorrectionType:UITextAutocorrectionTypeNo];
[txtFld setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[txtFld setEnablesReturnKeyAutomatically:YES];
[txtFld setClearButtonMode:UITextFieldViewModeWhileEditing];
[txtFld setReturnKeyType:keyType];
[txtFld setPlaceholder:placeholder];
[txtFld setFont:[UIFont fontWithName:@"Helvetica" size:18]];
return txtFld;
}
上面的代码运行良好,我得到了如下的输出表视图
但是当我尝试滚动 UITableView 时,UITextField 中的占位符值被折叠并显示如下
同时,当我尝试输入 textField 时,文本已与占位符文本重叠,如下所示
所以任何对此有想法的人请帮助我摆脱这个问题。提前致谢..