0

我在集成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

UITextField 响应 textFieldShouldBeginEditing

但是当我尝试滚动 UITableView 时,UITextField 中的占位符值被折叠并显示如下 占位符被折叠

同时,当我尝试输入 textField 时,文本已与占位符文本重叠,如下所示 占位符文本与文本字段文本重叠

所以任何对此有想法的人请帮助我摆脱这个问题。提前致谢..

4

2 回答 2

0

正如 PJ 指出的那样,问题是,您正在将文本字段添加到已经具有文本字段的单元格中,从而导致您看到的效果。

您可以使用以下方法轻松解决此问题

  1. 将 TAG 字段添加到文本字段时,将其添加为子视图,然后在每次重用单元格时使用该标签删除该文本字段

在你的标题中声明

#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]];
    }
    }
.

.
于 2013-07-29T15:36:46.787 回答
0

你以错误的方式编程。

您一次又一次地创建单元格并分配文本字段,因此它们相互覆盖。

有 2 个选项可以解决此问题:-

  1. 您可以创建自定义单元格类并在此处声明所有文本字段,然后在此处传递值。
  2. 您每次都需要创建单元格。

希望此解决方案对您有所帮助。

如果您选择 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;
}
于 2012-10-12T06:23:56.223 回答