2011-05-30 52 views
0

我試圖在tableview中添加textfields。除了最後一行,我想在每行中都有一個標籤和一個文本字段。我想要在最後一行的開關。 問題是文本字段在其餘行上重疊。我提出我的文本框代碼內,如果(細胞==無)但沒有奏效......這裏是我的代碼iphone,在UITableView中的UITextField

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
NSString *MyIdentifier = @"mainMenuIdentifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; 
    [cell setSelectedBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"highlightstrip.png"]]]; 


    if (tableView.tag == 1) { 

     UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 90, 20)]; 
     lblName.textAlignment = UITextAlignmentLeft; 
     lblName.font = [UIFont boldSystemFontOfSize:14]; 
     lblName.backgroundColor = [UIColor clearColor]; 
     lblName.tag = 31; 
     [cell.contentView addSubview:lblName]; 
     [lblName release]; 

    } 

} 

    if (tableView.tag == 1) { 

     [(UILabel *) [cell viewWithTag:31] setText:[tableElements objectAtIndex:indexPath.row]]; 
// check if the last row 
     if (indexPath.row == 10) { 
      newsSwtich = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease]; 
      [newsSwtich addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside]; 
      cell.accessoryView = newsSwtich; 
     } 
     else { 

       UITextField *tempTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 10, 200, 20)]; 
       tempTextField.delegate = self; 
       // tempTextField.placeholder = [tableElements objectAtIndex:indexPath.row]; 
       tempTextField.font = [UIFont fontWithName:@"Arial" size:14]; 
       tempTextField.textAlignment = UITextAlignmentLeft; 
       tempTextField.tag = indexPath.row; 
       tempTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support 
       tempTextField.keyboardType = UIKeyboardTypeDefault; // type of the keyboard 
       tempTextField.returnKeyType = UIReturnKeyDone; // type of the return key 
       tempTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right 
       [cell.contentView addSubview:tempTextField]; 
       [tempTextField release]; 


      cell.accessoryView = UITableViewCellAccessoryNone; 
     } 


cell.selectionStyle = UITableViewCellSelectionStyleNone; 
return cell; 

} 

當我向上和向下滾動文本字段重疊,我的意思後,我在第一行輸入文本並向下滾動,我可以看到複製到最後一行的文本字段。

回答

1

單元重用導致文本字段重疊。每次單元重新使用時,您都要添加一個文本字段或一個開關。這些堆積如山。在添加一個之前,您需要刪除舊的子視圖,該視圖既可以是開關,也可以是文本框。

+0

感謝您的答覆,但我怎麼能刪除舊子視圖??? – donito 2011-05-31 07:51:56

+0

在創建文本字段/開關時,爲其分配一個標籤。說45.然後使用'UIView * theView = [cell viewWithTag:45];'檢索它。從現在開始你已經看到了,只要做'[theView removeFromSuperview];'。或者,如果它是文本字段,並且您需要插入文本字段,則可以將其設置重置爲所需內容。這樣你可以刪除重新創建它的需要。 – 2011-05-31 09:23:55

0

爲什麼要使用兩個tableviews只是這樣做

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {       
if(indexPath.row==[tableviewarray count]){ 
//dont add label or textfield 
} 
else{ 
// add label or textfield 
} 

}