2013-07-03 56 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 /* ... */};