2011-12-01 72 views
0

我在UITableview分组样式中有两个部分有一个问题。第一部分有6排,第二部分有3排。当我上下滚动tableview时,第2节的最后一行内容将添加到第一节的第一行,并且第一节的第一行内容将添加到第二节的最后一行。我最好检查一下自己的级别,以确定单元格是否为空来加载内容,但在调试时不会发生。我的代码是,UITableview部分覆盖在iPhone?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
      cell.backgroundColor = [UIColor whiteColor]; 

      if (indexPath.section == 0 && indexPath.row == 0) 
      { 
       UILabel *Nameabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)]; 
       Nameabel.text = @"Name"; 
       Nameabel.font = [UIFont fontWithName:@"Helvetica" size:15]; 
       Nameabel.backgroundColor = [UIColor clearColor]; 
       Nameabel.font = [UIFont boldSystemFontOfSize:15]; 
       [cell.contentView addSubview:Nameabel]; 

       textField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)]; 
       textField.placeholder = @"Student Name"; 
       textField.borderStyle = UITextBorderStyleNone; 
       textField.font = [UIFont fontWithName:@"Helvetica" size:14]; 
       [cell.contentView addSubview:paymentCatTextField]; 

      } 
      else if (indexPath.section == 1 && indexPath.row == 0) 
      { 
       UILabel *RLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)]; 
       RLabel.text = @"Class"; 
       RLabel.backgroundColor = [UIColor clearColor]; 
       RLabel.font = [UIFont fontWithName:@"Helvetica" size:15]; 
       RLabel.font = [UIFont boldSystemFontOfSize:15]; 
       [cell.contentView RLabel]; 

       RTextField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)]; 
       RTextField.placeholder = @"class Name"; 
       RTextField.borderStyle = UITextBorderStyleNone; 
       RTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; 
       RTextField.font = [UIFont fontWithName:@"Helvetica" size:14]; 
       [cell.contentView addSubview:RTextField]; 
      } 
     } 
     return cell; 
    } 

这是为了示例。这样,部分0有6行,部分1有3行。我在哪里做错了。请帮我解决这个问题。

在此先感谢。

+0

细胞可重复使用,以提高性能,您应仔细阅读苹果的文档,并为UITableView的部分基本上是和重要性。 –

回答

1

你的代码更改为以下,然后再试一次:

{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.backgroundColor = [UIColor whiteColor]; 
    } 


    if (indexPath.section == 0 && indexPath.row == 0) { 
     //bla bla 
    } else if (indexPath.section == 1 && indexPath.row == 0) { 
     //bla bla 
    } 

    return cell; 
} 

的原因是您正在设置只有当你不重用单元格中的单元格内容。在大多数情况下这是错误的。

0

除了dasdom的回答,我还建议在添加标签之前删除子视图。

if ([[cell.contentView subviews] count] > 0) { 
    UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0]; 
    [labelToClear removeFromSuperview]; 
    UIView *textToClear = [[cell.contentView subviews] objectAtIndex:1]; 
    [textToClear removeFromSuperview]; 
} 

,并自动释放子视图:

UILabel *Nameabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)] autorelease]; 
UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)] autorelease];