2012-07-16 66 views
0

这是使用在“的cellForRowAtIndexPath”UITableView的重复行的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString* cellIdentifier = @"OBBirthControlMethodsTableCell"; 
    OBCustomDetailCell *cell = (OBCustomDetailCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    NSLog(@"cell id - %@",cell.subviews); 
    CGRect frame = [tableView rectForRowAtIndexPath:0]; 
    if(nil == cell) 
    { 
     cell = [[[OBCustomDetailCell alloc] 
       initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 

     if (indexPath.row != 3) 
     { 
      //Setting the basic template 
      UIView *template = [[UIView alloc] init]; 
      template.tag = indexPath.row+10; 
      NSLog(@"path index = %d",indexPath.row); 
      UIImageView *templateImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 
                         200, 
                         frame.size.height)]; 
      UILabel *templateLabel = [[UILabel alloc] initWithFrame:CGRectMake(templateImage.frame.size.width+20, 
                       0, 
                       cell.frame.size.width - templateImage.frame.size.width+20, 
                       frame.size.height)]; 
      [template addSubview:templateImage]; 
      [template addSubview:templateLabel]; 

      [cell.contentView addSubview:template]; 
     } 
    } 

    UIView *templateView = [cell.contentView viewWithTag:indexPath.row + 10]; 
    if (templateView) 
    { 
     NSLog(@"Gotten a templateView object"); 
     if (indexPath.row == 0) 
     { 
      templateView.frame = frame; 

      for (UIView *view in templateView.subviews) 
      { 
       if ([view isKindOfClass:[UIImageView class]]) 
       { 
        [(UIImageView *)view setImage:[UIImage imageNamed:@"baby.jpeg"]]; 
       } 
       else if ([view isKindOfClass:[UILabel class]]) 
       { 
        [(UILabel *)view setText:@"This is not working"]; 
       } 
      } 
     } 
     else 
     { 
      templateView.frame = CGRectMake(0, 50, 
              frame.size.width, 
              frame.size.height); 

     } 
    } 
    return cell; 
} 

代码IM但问题是,新的电池给我相同的值OS旧的小区是出列,一旦你的新细胞向下滚动..

编辑 重复的细胞正在创建只要我们向下滚动,与第一小区相同瓦莱斯一个新的小区..

  • 我想创建仅用于选择行()..if(indexPath.row!= 3)的UIView
  • 我想在某些行中UIView的位置不同。if (indexPath.row == 0)
+0

什么是不工作?文字没有被更新? 'indexPath.row == 3'上的单独单元格不工作?它是什么? – 2012-07-16 06:20:19

+0

我已经添加了更清晰的问题解释 – 2012-07-16 06:23:03

+0

**您何时以及何时从单元格中移除了不需要的**模板**视图?**因为已出队的单元格具有此视图并且您似乎添加了两个或更多为相同的单元格添加模板“UIView”的新实例,而不删除它们中存在的一个。你只是在浪费记忆,而你却让自己头痛。你为什么不为不同种类的细胞使用不同'cellID'的不同定制细胞?这个解决方案非常粗糙... – holex 2012-07-17 10:31:42

回答

1

这段代码有几个问题。首先,你的问题的主要原因是这样的:

template.tag = indexPath.row+10; 

你为什么要这样做?只需使用一个常数值,如10。无需涉及索引路径,因为每个单元格都会发生变化。这将导致viewWithTag:重用单元失败,并且它将返回nil。其次,您不仅可以将您的模板单元设置为indexPath.row != 3,因为在某些时候,非模板单元可能会被重新使用。它将不具有模板视图,因此以下布局将失败。您需要为两种类型的单元使用两个重用标识符。最终产品应该看起来像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *templateCellIdentifier = @"OBBirthControlMethodsTableCell"; 
    static NSString *otherCellIdentifier = @"OtherTableCell"; 

    if (indexPath.row != 3) { 
     // Handle normal cells 
     OBCustomDetailCell *cell = (OBCustomDetailCell *)[tableView dequeueReusableCellWithIdentifier:templateCellIdentifier]; 
     if (cell == nil) { 
      cell = [[[OBCustomDetailCell alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:templateCellIdentifier] autorelease]; 
      // Set up template cell 
     } 
     // Handle per-cell data 
    } else { 
     // Handle special cells 
     OBCustomDetailCell *cell = (OBCustomDetailCell *)[tableView dequeueReusableCellWithIdentifier:otherCellIdentifier]; 
     if (cell == nil) { 
      cell = [[[OBCustomDetailCell alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:otherCellIdentifier] autorelease]; 
      // Set up other cell 
     } 
     // Handle per-cell data (not really necessary if there's only one of these) 
    } 
}