UITextField在UITableViewCell中无法正常工作

时间:2012-10-12 06:14:27

标签: objective-c ios5 uitableview uitextfield uitextfielddelegate

我在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如下 UITextField within UITableView

UITextField responding to textFieldShouldBeginEditing

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

在我尝试输入textField的同时,文本已与占位符文本重叠,如下所示 placeholder text overlaps with textfield text

所以任何有此想法的人都可以帮我解决这个问题。 提前谢谢..

2 个答案:

答案 0 :(得分:0)

你的编程错误。

您正在反复创建一次单元格和分配文本字段,因此它们会相互重叠。

有两种方法可以解决这个问题: -

  1. 您可以创建自定义单元格类并在其中声明所有文本字段,只需在此处传递值。
  2. 您每次都需要创建单元格。
  3. 希望此解决方案可以帮助您。

    如果您选择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指出的那样,问题是,您正在将文本字段添加到已经有文本字段的单元格中,从而产生您所看到的效果。

您可以使用

轻松解决此问题
  1. 将TAG字段添加为文本字段时,将其添加为子视图,然后在每次重复使用单元格时使用标记删除该文本字段
  2. 在标题声明中

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