的UIView表示一个UITableView部向上它从未被添加滚动

问题描述:

当我已经得到了我添加至细胞的内容视图中的特定部分(部分1特异性)一个UIView到,如下所示:的UIView表示一个UITableView部向上它从未被添加滚动

[cell.contentView addSubview:self.overallCommentViewContainer]; 

当我快速向上/向下滚动 - UIView的出现在第0 - 尽管我从来没有加入条文UIView的任何单元格的0

这里有一个详细的看看我cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *kCustomCellID = @"CustomCellID"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCustomCellID] autorelease]; 
    } 

    // Configure the cell. 
    switch(indexPath.section) { 

     case 0: 
      // other code 
      break; 
     case 1: 

      // add the overall comment view container to the cell 
      NLog(@"adding the overallCommentViewContainer"); 
      [cell.contentView addSubview:self.overallCommentViewContainer]; 
      NLog(@"creating the row at: Section %d, Row %d", indexPath.section, indexPath.row); 
      break; 
    } 
    return cell; 
} 
+1

我认为这与细胞再利用有关。我不能确定提供的答案是 – 2009-12-04 18:14:07

如果UITableView的单元已准备好重用,则其dequeueReusableCellWithIdentifier方法将愉快地返回第1节中最初使用的第0节的单元格!我建议这样的事情你让他们分开:

UITableViewCell *cell; 

// Configure the cell. 
switch(indexPath.section) { 

    case 0: 
     cell = [tableView dequeueReusableCellWithIdentifier:@"Section0Cell"]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Section0Cell"] autorelease]; 
     } 
     // other code 
     break; 
    case 1: 
     cell = [tableView dequeueReusableCellWithIdentifier:@"Section1Cell"]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Section1Cell"] autorelease]; 
      // add the overall comment view container to the cell 
      NLog(@"adding the overallCommentViewContainer"); 
      [cell.contentView addSubview:self.overallCommentViewContainer]; 
     } 
     NLog(@"creating the row at: Section %d, Row %d", indexPath.section, indexPath.row); 
     break; 
} 
return cell; 

的关键是使用不同的标识字符串为每种类型的细胞的您使用的是不与表中的其他细胞可以互换。

+1

,但效果很好 - 现在一切都保持原位!谢谢! – Cole 2009-12-08 19:55:49

+0

非常好,谢谢你回报! – 2009-12-08 20:26:41