2011-02-02 56 views
0

你好 我正在使用分组tableview来填充文本字段中的条目。我使用一节和9行。 并将一个标签和文本字段与占位符一起放置。它正在重复行,当我开始编辑文本字段时,它不清除占位符它重写。而且我用开关还当我滚动第一行自动显示开关,我用在最后一行。whats错我的代码? 这里是我的代码...Iphone中的分组表视图,带有一个部分

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return 9; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 



     if (indexPath.row==0) 
     { 


     UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)]; 
     [Name setText:@"First Name "]; 
     [cell.contentView addSubview:Name]; 

     UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
     //[email protected]"Enter First Name"; 
     [textName setBorderStyle:UITextBorderStyleNone]; 
     textName.clearButtonMode = UITextFieldViewModeWhileEditing; 
     textName.keyboardType=UIKeyboardTypeDefault; 
     textName.returnKeyType=UIReturnKeyDone; 
     textName.delegate=self; 
     textName.clearsOnBeginEditing=YES; 
     [cell.contentView addSubview:textName]; 


     } 



     if (indexPath.row==1) 
     { 

      UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 20)]; 
      [Name setText:@"Middle Name"]; 
      [cell.contentView addSubview:Name]; 

      UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
      [email protected]"Enter Middle Name"; 
      [textName setBorderStyle:UITextBorderStyleNone]; 
      textName.clearButtonMode = UITextFieldViewModeWhileEditing; 
      textName.keyboardType=UIKeyboardTypeDefault; 
      textName.returnKeyType=UIReturnKeyDone; 
      textName.delegate=self; 
      [cell.contentView addSubview:textName]; 


     } 

     if (indexPath.row==2) 
     { 
      UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)]; 
      [Name setText:@"Last Name "]; 
      [cell.contentView addSubview:Name]; 

      UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
      [email protected]"Enter Last Name"; 
      [textName setBorderStyle:UITextBorderStyleNone]; 
      textName.clearButtonMode = UITextFieldViewModeWhileEditing; 
      textName.keyboardType=UIKeyboardTypeDefault; 
      textName.returnKeyType=UIReturnKeyDone; 
      textName.delegate=self; 
      [cell.contentView addSubview:textName]; 


     } 



     if (indexPath.row==3) { 


      UILabel *Payment=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 150, 20)]; 
      [Payment setText:@"Credit Card"]; 
      [cell.contentView addSubview:Payment]; 


      paymentText=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
      [email protected]"Payment Type"; 
      [paymentText setBorderStyle:UITextBorderStyleNone]; 
      paymentText.clearButtonMode = UITextFieldViewModeWhileEditing; 
      paymentText.clearsOnBeginEditing=YES; 
      //paymentText.keyboardType=UIKeyboardTypeDefault; 
      //paymentText.returnKeyType=UIReturnKeyDone; 
      //paymentText.delegate=self; 


      picker = [[UIPickerView alloc] initWithFrame:CGRectZero]; 
      const int kPaymentPickerTag = 3002; 
      //picker.hidden=NO; 
      picker.tag = kPaymentPickerTag; 
      picker.delegate = self; 
      picker.dataSource = self; 

      [picker setShowsSelectionIndicator:YES]; 


      //display picker on textfield payment 

      //paymentText.inputView = picker; 
      paymentText.inputView=picker; 

     // paymentText.inputAccessoryView=picker; 




      //[cell.contentView addSubview:PaymentPicker]; 


      [cell.contentView addSubview:paymentText]; 
     } 

     if (indexPath.row==4) { 


      UILabel *Card=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 150, 20)]; 
      [Card setText:@"Card Number"]; 
      [cell.contentView addSubview:Card]; 


      UITextField *CardText=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
      [email protected]"Enter Card Number"; 
      [CardText setBorderStyle:UITextBorderStyleNone]; 
      CardText.clearButtonMode = UITextFieldViewModeWhileEditing; 
      CardText.keyboardType=UIKeyboardTypeNumbersAndPunctuation; 
      CardText.returnKeyType=UIReturnKeyDone; 
      CardText.delegate=self; 
      CardText.clearsOnBeginEditing=YES; 
      [cell.contentView addSubview:CardText]; 
     } 
     if (indexPath.row==5) { 


      UILabel *date=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 150, 20)]; 
      [date setText:@"Exp Date"]; 
      [cell.contentView addSubview:date]; 


      DateText=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
      [email protected]"Select Date"; 
      [DateText setBorderStyle:UITextBorderStyleNone]; 
      DateText.clearButtonMode=UITextFieldViewModeWhileEditing; 
      DateText.clearsOnBeginEditing=YES; 

      UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectZero]; 
      datePicker.datePickerMode = UIDatePickerModeDate; 
      [datePicker addTarget:self action:@selector(DateChanged) forControlEvents:UIControlEventValueChanged]; 

      NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
      dateComponents.year = 1980; 
      dateComponents.month = 1; 
      dateComponents.day = 1; 
      NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
      datePicker.date = [gregorian dateFromComponents:dateComponents]; 
      [dateComponents release]; 
      [gregorian release]; 

      //date picker on textfield 
      DateText.inputView = datePicker; 
      [datePicker release]; 


      [cell.contentView addSubview:DateText]; 
     } 
     if (indexPath.row==6) { 


      UILabel *Amt=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 150, 20)]; 
      [Amt setText:@"Amount"]; 
      [cell.contentView addSubview:Amt]; 

      UITextField *AmtText=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
      [email protected]"Enter Amount"; 
      [AmtText setBorderStyle:UITextBorderStyleNone]; 
      AmtText.clearButtonMode = UITextFieldViewModeWhileEditing; 
      AmtText.keyboardType=UIKeyboardTypeNumbersAndPunctuation; 
      AmtText.returnKeyType=UIReturnKeyDone; 
      AmtText.delegate=self; 
      AmtText.clearsOnBeginEditing=YES; 
      [cell.contentView addSubview:AmtText]; 
     } 
     if (indexPath.row==7) { 


      UILabel *address=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 150, 20)]; 
      [address setText:@"Billing Address"]; 
      [cell.contentView addSubview:address]; 


      UITextField *addressText=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
      addressT[email protected]"Enter Address"; 
      [addressText setBorderStyle:UITextBorderStyleNone]; 
      addressText.clearButtonMode = UITextFieldViewModeWhileEditing; 
      addressText.keyboardType=UIKeyboardTypeDefault; 
      addressText.returnKeyType=UIReturnKeyDone; 
      addressText.delegate=self; 
      [cell.contentView addSubview:addressText]; 
      addressText.clearsOnBeginEditing=YES; 
      /*UIButton* pencil = [UIButton buttonWithType:UIButtonTypeCustom]; 
      [pencil setImage:[UIImage imageNamed:@"plus.jpg"] forState:UIControlStateNormal]; 
      pencil.frame = CGRectMake(0, 0, 40, 40); 
      pencil.userInteractionEnabled = YES; 
      [pencil addTarget:self action:@selector(didTapEditButton:) forControlEvents:UIControlEventTouchDown]; 
      cell.accessoryView = pencil  ; 
      */ 
      cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton; 

     } 
     if (indexPath.row==8) { 

      UILabel *Gift=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 150, 20)]; 
      [Gift setText:@"Gift Receipt"]; 
      [cell.contentView addSubview:Gift]; 


      GiftText=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 180, 20)]; 

      [email protected]"Yes"; 
      [GiftText setBorderStyle:UITextBorderStyleNone]; 
      GiftText.clearsOnBeginEditing=YES; 
      GiftText.returnKeyType=UIReturnKeyDone; 
      GiftText.delegate=self; 
      [cell.contentView addSubview:GiftText]; 

      switchView=[[UISwitch alloc]initWithFrame:CGRectMake(140, 5, 30, 10)]; 

      switchView=[UISwitch switchWithLeftText:@"Yes" andRight:@"No"]; 
      switchView.on=YES; 

      [cell.contentView addSubview:switchView]; 


      cell.accessoryView=switchView; 

      [(UISwitch *)cell.accessoryView addTarget:self action:@selector(mySelector) forControlEvents:UIControlEventValueChanged]; 

     } 






    // Configure the cell... 

    return cell; 
} 

回答

0
 UILabel *Name; 
    UITextField *textName; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

    Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)]; 
    Name.tag =1001; 
    [Name setText:@"First Name "]; 
    [cell.contentView addSubview:Name]; 

    textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)]; 
    //[email protected]"Enter First Name"; 
    textName.tag =1002 
    [textName setBorderStyle:UITextBorderStyleNone]; 
    textName.clearButtonMode = UITextFieldViewModeWhileEditing; 
    textName.keyboardType=UIKeyboardTypeDefault; 
    textName.returnKeyType=UIReturnKeyDone; 
    textName.delegate=self; 
    textName.clearsOnBeginEditing=YES; 
    [cell.contentView addSubview:textName]; 

    }else{ 
    Name = (UILable*)[cell.contentview viewwithtag:1001]; 
    textName = (UItextField*)[cell.contentview viewwithtag:1002]; 
    } 

    if(indexpath.row==0){ 
    [email protected]"aaa"; 
    textName [email protected]"bbb"; 
    }... 
    . 
    . 
    . 
+0

然后我就试试这个 – 2011-02-02 13:10:31