2011-02-02 54 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