我在將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如下
但是當我試圖滾動的UITableView中的UITextField的佔位符值獲得的崩潰,它會顯示如下
與此同時,當我試圖鍵入文本框的文本已與佔位符文字重疊如下
因此,任何人有這方面的想法,請幫助我擺脫這個問題。 在此先感謝..
感謝您的回覆。正如你所說的選項2可能會幫助我編碼和更新答案.. – Yuva
我編輯了我的答案,我粘貼了代碼。 –
我已經實現了你提到的相同的代碼,但現在重複的佔位符值,即langaugename,地區,城市,學校,研究,再次發送同樣的順序重複...然後佔位符崩潰時,我滾動tableview,輸出類似於我的問題中的圖片3。 – Yuva