2013-07-17 20 views
3

新手ios的問題。我正在嘗試使用表格視圖控制器創建註冊表單。我試圖在cellForRowAtIndexPath方法中以編程方式向每個單元格添加textfields,但是所有文本字段似乎都在第一個單元格上創建 - 一個單元格與另一個單元格重疊。這是我的代碼。ios - 添加文本字段到uitableviewcell編程不工作

這是我的細胞如何呈現。我認爲我正在對重複使用單元的部分做些愚蠢的事情。我沒有檢查一些其他類似的線程在stackoverflow,但沒有一個幫助。你能告訴我我錯過了什麼,或者這是否應該這樣做。您的意見非常感謝。

謝謝, 邁克

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"signUpFields" forIndexPath:indexPath]; 

// Configure the cell... 
if (indexPath.row == 0){ 
    //self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)]; 
    self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
    self.firstName.placeholder = @"First Name"; 
    self.firstName.autocorrectionType = UITextAutocorrectionTypeNo; 
    [self.firstName setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    //cell.accessoryView = self.firstName; 
    [cell.contentView addSubview:self.firstName]; 
} 

if (indexPath.row == 1){ 
    self.lastName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
    self.lastName.placeholder = @"Last Name"; 
    self.lastName.autocorrectionType = UITextAutocorrectionTypeNo; 
    [self.lastName setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    //cell.accessoryView = self.lastName; 
    [cell.contentView addSubview:self.lastName]; 
} 

if (indexPath.row == 2){ 
    self.emailId = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
    self.emailId.placeholder = @"Email Id"; 
    self.emailId.autocorrectionType = UITextAutocorrectionTypeNo; 
    [self.emailId setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    //cell.accessoryView = self.emailId; 
    [cell.contentView addSubview:self.emailId]; 
} 

if (indexPath.row == 3){ 
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
    self.password.placeholder = @"Password"; 
    self.password.secureTextEntry = YES; 
    self.password.autocorrectionType = UITextAutocorrectionTypeNo; 
    [self.password setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    //cell.accessoryView = self.password; 
    [cell.contentView addSubview:self.password]; 
} 

self.firstName.delegate = self; 
self.lastName.delegate = self; 
self.emailId.delegate = self; 
self.password.delegate = self; 

[self.signUpTable addSubview:self.firstName]; 
[self.signUpTable addSubview:self.lastName]; 
[self.signUpTable addSubview:self.emailId]; 
[self.signUpTable addSubview:self.password]; 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 
return cell; 

} 
+0

你爲什麼要將你的UITextFields添加到tableView? –

+0

爲什麼不創建自定義的UITableViewCell?並執行prepareForReuse函數來重置單元格數據。更多信息在蘋果文檔 http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html –

回答

5

試試這個..

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 

     if (indexPath.row == 0){ 
    //self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)]; 
    self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
    self.firstName.placeholder = @"First Name"; 
    self.firstName.autocorrectionType = UITextAutocorrectionTypeNo; 
    [self.firstName setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    //cell.accessoryView = self.firstName; 
    [cell addSubview:self.firstName]; 
} 

if (indexPath.row == 1){ 
    self.lastName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
    self.lastName.placeholder = @"Last Name"; 
    self.lastName.autocorrectionType = UITextAutocorrectionTypeNo; 
    [self.lastName setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    //cell.accessoryView = self.lastName; 
    [cell addSubview:self.lastName]; 
} 

if (indexPath.row == 2){ 
    self.emailId = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
    self.emailId.placeholder = @"Email Id"; 
    self.emailId.autocorrectionType = UITextAutocorrectionTypeNo; 
    [self.emailId setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    //cell.accessoryView = self.emailId; 
    [cell addSubview:self.emailId]; 
} 

if (indexPath.row == 3){ 
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
    self.password.placeholder = @"Password"; 
    self.password.secureTextEntry = YES; 
    self.password.autocorrectionType = UITextAutocorrectionTypeNo; 
    [self.password setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    //cell.accessoryView = self.password; 
    [cell addSubview:self.password]; 
} 

self.firstName.delegate = self; 
self.lastName.delegate = self; 
self.emailId.delegate = self; 
self.password.delegate = self; 


cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     return cell; 
    } 

無需重新添加在表視圖文本視圖這是錯誤的。試試這個會對你有用,但是你最好創建自定義Cell並且使用它是最好的方法,因爲你可以使用Cell想要的任何東西。

+0

這工作。謝謝! –

4

不要將其添加爲子視圖,將它們設置爲電池的附件視圖。

- (UITextField*)getTextField{ 
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 20, 35)]; 
    tf.delegate = self; 
    tf.textColor  = [UIColor colorWithRed:.231 green:.337 blue:.533 alpha:1]; 
    tf.autocorrectionType = UITextAutocorrectionTypeNo; 
    tf.borderStyle = UITextBorderStyleNone; 
    tf.frame = CGRectMake(0, 20, 170, 30); 
    tf.clearButtonMode = UITextFieldViewModeWhileEditing; 
    tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    tf.font = [UIFont systemFontOfSize:13]; 
    return tf; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
     cell.textLabel.font = [UIFont systemFontOfSize:13]; 
     cell.detailTextLabel.font = [UIFont systemFontOfSize:13]; 
     cell.detailTextLabel.numberOfLines = 2; 
    } 

    if (indexPath.section == 0) { 

     UITextField *tf = (UITextField*)cell.accessoryView; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.textLabel.numberOfLines = 2; 
     tf     = [self getTextField]; 
     cell.accessoryView = cell.editingAccessoryView = tf; 
     [((UITextField*)cell.accessoryView) setBorderStyle:self.tableView.editing ? UITextBorderStyleRoundedRect : UITextBorderStyleNone]; 
     [((UITextField*)cell.accessoryView) setUserInteractionEnabled:self.tableView.editing ? YES : NO]; 
     [((UITextField*)cell.accessoryView) setTextAlignment:!self.tableView.editing ? UITextAlignmentRight : UITextAlignmentLeft]; 
     ((UITextField*)cell.accessoryView).tag = indexPath.row; 
    } 

    return cell; 
} 

的想法是,電池每次出現在屏幕上,從關閉屏幕來了,如果你跟addSubview:添加文本字段,它每次都會添加它,以及時間重新繪製。你可以這樣做,但是你必須清除單元的子視圖的contentView,但這需要額外的工作,CPU和內存使用,而不是說這是最不優雅的解決方案。

0

保持這一個和嘗試,

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *MyIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.row,indexPath.section]; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) 
    { 
    //your stuff. 
    } 
} 
0

嘗試使用這一個...而且也沒有必要將這些文本框添加到tableview中。所以,請刪除一些代碼..

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"signUpFields" forIndexPath:indexPath]; 

// Configure the cell... 
if (indexPath.row == 0){ 
    //self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)]; 
    self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
    self.firstName.placeholder = @"First Name"; 
    self.firstName.autocorrectionType = UITextAutocorrectionTypeNo; 
    [self.firstName setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    //cell.accessoryView = self.firstName; 
    [cell addSubview:self.firstName]; 
} 

if (indexPath.row == 1){ 
    self.lastName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
    self.lastName.placeholder = @"Last Name"; 
    self.lastName.autocorrectionType = UITextAutocorrectionTypeNo; 
    [self.lastName setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    //cell.accessoryView = self.lastName; 
    [cell addSubview:self.lastName]; 
} 

if (indexPath.row == 2){ 
    self.emailId = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
    self.emailId.placeholder = @"Email Id"; 
    self.emailId.autocorrectionType = UITextAutocorrectionTypeNo; 
    [self.emailId setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    //cell.accessoryView = self.emailId; 
    [cell addSubview:self.emailId]; 
} 

if (indexPath.row == 3){ 
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)]; 
    self.password.placeholder = @"Password"; 
    self.password.secureTextEntry = YES; 
    self.password.autocorrectionType = UITextAutocorrectionTypeNo; 
    [self.password setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    //cell.accessoryView = self.password; 
    [cell addSubview:self.password]; 
} 

self.firstName.delegate = self; 
self.lastName.delegate = self; 
self.emailId.delegate = self; 
self.password.delegate = self; 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 
return cell; 

} 
0

而不是寫更多的細胞只是創建一個定製的細胞與textfield.Give每個文本框的標籤,還可以設置行的numberOfRowsIntheSections計數,然後它會顯示電池多少錢你需要。我認爲它會幫助你。

0

在你按照我的回答之前,我想告訴你下面的代碼對內存管理不好,因爲它會爲每行UITableView創建一個新的單元,所以要小心。

但是最好使用,當UITableView有限行(大約50-100可能)那麼下面的代碼對你的情況有幫助,使用它,如果它適合你。

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

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 


     /// Put/Create your UITextField or any Controls here 


    } 

    return cell; 
} 
2

看起來你只有四個單元要顯示。這是一個靜態的情況,細胞不會改變,你應該在xib/storyboard中靜態創建這個tableView和它的單元格。這是這裏的首選方式。

如果您確實想要以編程方式執行此操作,請使用您想要的行爲創建四個UITableViewCells。讓他們在一個數組中。內部cellForRowAtIndex路徑return cellArray[indexPath.row];

注意這是最好的方法,因爲你只有四個tableViewCells。 只有當您擁有的細胞數量超過一次可以容納在屏幕上時,ReuseIdentifiers纔會派上用場。所以在你的情況下,你永遠不會重複使用這些單元格。

-1

@Mike:首先,我會建議你使用StoryBoard或Nib文件。

如果你使用它,那麼你可以使用下面的代碼

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    UITextField *txtField = (UITextField *)[cell viewWithTag:1]; 
    txtField.text = @"Latest"; 

    return cell; 
} 

基礎上你可以設置文本行數。

如果您想在運行時添加UITextField。那麼你可以使用下面的代碼。

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(165.0, 7.0, 150.0, 30.0)]; 
    txtField.text = @"Latest"; 
    [cell.contentView addSubview:txtField]; 
    return cell; 
} 

您可以分配不同的標籤並根據標籤存儲這些值。