0

我在將UITextfield作爲UITableViewCell中的子視圖進行集成時遇到問題。我用下面的代碼在我的項目的UITextField內UITableViewCell的UITextField在UITableViewCell中無法正常工作

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *[email protected]"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; 
} 

添加作爲一個子視圖添加文本字段到表單元格中我使用了自定義的功能如下

-(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

與此同時,當我試圖鍵入文本框的文本已與佔位符文字重疊如下 placeholder text overlaps with textfield text

因此,任何人有這方面的想法,請幫助我擺脫這個問題。 在此先感謝..

回答

0

您編程錯誤。

您一次創建單元格並分配文本字段,因此它們互相重疊。

有2個選項來解決這個問題: -

  1. 您可以創建自定義單元格級,並聲明所有文本字段有,只是傳遞值這裏。
  2. 您需要每次創建單元格。

希望這個解決方案可以幫助你。

如果您選擇了2一個

這裏是代碼: -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *[email protected]"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; 
} 
+0

感謝您的回覆。正如你所說的選項2可能會幫助我編碼和更新答案.. – Yuva

+0

我編輯了我的答案,我粘貼了代碼。 –

+0

我已經實現了你提到的相同的代碼,但現在重複的佔位符值,即langaugename,地區,城市,學校,研究,再次發送同樣的順序重複...然後佔位符崩潰時,我滾動tableview,輸出類似於我的問題中的圖片3。 – Yuva

0

由於PJ指出,問題是,你要添加文本框到已有文本框的細胞產生的影響你看。

您可以輕鬆地解決這個使用

  1. 標籤字段添加到文本框,當你添加它作爲一個子視圖,然後使用標籤,每次刪除該文本框,當您重新使用細胞

在您的標題聲明

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

. 
相關問題