2013-07-03 77 views
1

我正在爲iPhone應用程序做一個註冊表單。在那裏有一個tableview有四節。最後一節是有一行註冊按鈕。我得到了tableview與數據well.But而滾動tableview上下我的註冊按鈕去第二節。我添加了下面的代碼我在這做了什麼錯,幫助我解決。cellview order change while tableview scroll up and down

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
    { 
    // static NSString *CellIdentifier = @"Cell"; 
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     NSString *identifier = @"reuseIdentifier"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 

    NSLog(@" row and section ===== %d,%d",indexPath.row,indexPath.section); 
    if(indexPath.section != 3) 
    { 
     UILabel *lbl_title = [[UILabel alloc] initWithFrame:(isiPad?CGRectMake(7,10,250,50):CGRectMake(70,40,340, 35))]; 
     [lbl_title setFont:[UIFont fontWithName:@"Verdana-Bold" size:(isiPad?20:13)]]; 
     lbl_title.backgroundColor = [UIColor clearColor]; 
     [lbl_title setTextColor:[UIColor blackColor]]; 

     lbl_title.tag = ((indexPath.section+1)*10)+indexPath.row; 
     //NSLog(@"lbl data tag %d",lbl_title.tag); 

     lbl_title.textAlignment = UITextAlignmentLeft; 
     [cell.contentView addSubview:lbl_title];  

     UILabel *lbl_data = [[UILabel alloc] initWithFrame:(isiPad?CGRectMake(260,10,410,50):CGRectMake(70,40,340, 35))]; 
     [lbl_data setFont:[UIFont fontWithName:@"Verdana-Bold" size:(isiPad?20:13)]]; 
     lbl_data.backgroundColor = [UIColor clearColor];//25-25-112 
     [lbl_data setTextColor:[UIColor colorWithRed:25.0/255.0 green:25.0/255.0 blue:112.0/255.0 alpha:1.0]]; 
     lbl_data.textAlignment = UITextAlignmentCenter; 
     lbl_data.tag = ((indexPath.section+1)*100)+indexPath.row; 
     //NSLog(@"lbl data tag %d,%@",lbl_data.tag,lbl_data); 
     [cell.contentView addSubview:lbl_data]; 

     if(indexPath.section == 0) 
     { 
      if(indexPath.row == 0) 
      { 
       lbl_title.text = @"Email"; 
      } 
      else if(indexPath.row == 1) 
      { 
       lbl_title.text = @"Password"; 

      } 
      else if(indexPath.row == 2) 
      { 
       lbl_title.text = @"Confirm password"; 
      } 
     } 
     else if(indexPath.section == 1) 
     { 
      if(indexPath.row == 0) 
      { 
       lbl_title.text = @"First name"; 
      } 
      else if(indexPath.row == 1) 
      { 
       lbl_title.text = @"Last name"; 

      } 
      else if(indexPath.row == 2) 
      { 
       lbl_title.text = @"Nickname"; 
      } 
      else if(indexPath.row == 3) 
      { 
       lbl_title.text = @"Gender"; 
      }    
     } 
     else if(indexPath.section == 2) 
     { 
      if(indexPath.row == 0) 
      { 
       lbl_title.text = @"City"; 
      } 
      else if(indexPath.row == 1) 
      { 
       lbl_title.text = @"State"; 

      } 
      else if(indexPath.row == 2) 
      { 
       lbl_title.text = @"Country"; 
      } 
     } 

    } 
    if(indexPath.section == 3) 
    { 
     if(indexPath.row == 0) 
     { 
     cell.backgroundColor = [UIColor colorWithRed:165.0/255.0 green:42.0/255.0 blue:42.0/255.0 alpha:1.0];//165-42-42 
     UIButton *btn_register = [[UIButton alloc] initWithFrame:isiPad?CGRectMake(10,0,660,70):CGRectMake(0,0,320,40)]; 
     [btn_register addTarget:self action:@selector(btn_register_clicked:) forControlEvents:UIControlEventTouchUpInside]; 
     [btn_register setTitle:@"Register" forState:UIControlStateNormal]; 
     //btn_register.titleLabel.textAlignment = UITextAlignmentCenter; 
     btn_register.backgroundColor = [UIColor blueColor]; 
     [btn_register setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
     [btn_register setFont:[UIFont fontWithName:@"Verdana-Bold" size:24]]; 
     [cell.contentView addSubview:btn_register]; 
     } 
    } 

} 
else 
{ 
    NSLog(@"updating cell........."); 
} 
    return cell; 

    } 

before scrolling up and down

after scrolling

+1

看到我的回答.. http://stackoverflow.com/questions/14353280/when-tableview-scroll-than-value-changed/14353299#14353299 –

+0

我的其它具有相同問題的答案,你從這個鏈接階段http://stackoverflow.com/questions/15702242/uitableviewcells-with-default-image-overwritten-with-other-images-upon-scrolli/15702271#15702271 –

回答

1

它重複因爲您將它添加到單元格的contentview,然後您重新使用該單元格。 而不是將其添加到單元格的contentView中,將該按鈕添加爲單元格附件。

cell.accessoryView = btn_register; 

而在所有其他單元格中,將accessoryView設置爲nil。

cell.accessoryView = nil; 
0

你爲什麼不嘗試電池的每個部分設置不同的標識符。您也可以使用使用[tableView dequeueReusableCellWithIdentifier:forIndexPath:

0

對「註冊」單元使用不同的單元標識符應該工作。如果您使用容器來保存數據,您的代碼將非常清晰,如NSDictionary。例如:

NSArray *account = @[@"Email", @"Password", @"Confirm password"]; 
NSArray *userInfo = @[@"First name", @"Last name", @"Gender", @"Nickname"]; 
// ... 
NSDictionary *data = @{@"Account": account, @"User Information": userInfo /* ... */};