我在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添加到Table Cell中我使用了自定义函数,如下所示
-(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;
}
上面的代码工作正常,我得到输出tableview如下
但是当我试图滚动UITableView时,UITextField中的占位符值被折叠并显示如下
在我尝试输入textField的同时,文本已与占位符文本重叠,如下所示
所以任何有此想法的人都可以帮我解决这个问题。 提前谢谢..
答案 0 :(得分:0)
你的编程错误。
您正在反复创建一次单元格和分配文本字段,因此它们会相互重叠。
有两种方法可以解决这个问题: -
希望此解决方案可以帮助您。
如果您选择2个
这是代码: -
- (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;
}
答案 1 :(得分:0)
正如P.J指出的那样,问题是,您正在将文本字段添加到已经有文本字段的单元格中,从而产生您所看到的效果。
您可以使用
轻松解决此问题在标题声明中
#define MY_CUSTOM_TAG "MY_CUSTOM_TAG"
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
else
{
// Clear the old textfield before reusing it
[[cell.contentView viewWithTag:MY_CUSTOM_TAG]removeFromSuperview] ;
}
if (indexPath.row==languageName) {
if (langNameTxtFld==nil) {
langNameTxtFld=[[UITextField alloc]initWithFrame:CGRectMake(50,0, 265, 44)];
langNameTxtFld.delegate=self;
langNameTxtFld.tag = MY_CUSTOM_TAG; // Set a tag so it can be later found and removed for reusing this cell
[cell addSubview:[self addTextFieldWithText:nil textField:langNameTxtFld placeholder:@"Language Name" returnType:UIReturnKeyNext]];
}
}
.
.