2011-08-08 49 views
0

我正在尝试实现分组式样的UITableView,比如Contact apps detailedView。我想最顶层的单元格是透明的,并在底部有一个UISegemtedControl。使用多个自定义的UITableViewCells

当我尝试创建两种不同类型的自定义单元格时,即使使用两个不同的cellIdentifiers,也只加载第一个。

希望som指导。或者为同一主题提供一些很好的教程提示。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    /* 
    UIView *backView = [[UIView alloc] initWithFrame:CGRectZero]; 
    backView.backgroundColor = [UIColor clearColor]; 
    cell.backgroundView = backView; 
    [backView release]; 
    */ 

    static NSString *cellIdentifier1 = @"DetailCellStyle1"; 
    static NSString *cellIdentifier2 = @"DetailCellStyle2"; 

    if (indexPath.section == 0) { 

     // Load from nib 
     DetailCellViewController *cell = (DetailCellViewController *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 
     if (cell == nil) { 
      NSArray *topLevelObjects = [[NSBundle mainBundle] 
             loadNibNamed:@"DetailCellView" 
             owner:nil 
             options:nil]; 

      for (id currentObject in topLevelObjects) { 
       if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
        cell = (DetailCellViewController *) currentObject; 
        break; 
       } 
      } 
     } 

     return cell; 
    } 
    else { 
     // Load from nib 
     DetailCellViewController2 *cell = (DetailCellViewController2 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier2]; 
     if (cell == nil) { 
      NSArray *topLevelObjects = [[NSBundle mainBundle] 
             loadNibNamed:@"DetailCellView" 
             owner:nil 
             options:nil]; 

      for (id currentObject in topLevelObjects) { 
       if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
        cell = (DetailCellViewController2 *) currentObject; 
        break; 
       } 
      } 
     } 

     return cell; 
    } 

    return nil; 
} 
+0

你的'numberOfSectionsInTableView:'方法返回什么? – omz

+0

@omz它返回2. – Silversnail

+0

好吧,我假设你也许只有一个部分有两行...再次查看你的代码,我注意到你加载单元格1和单元格2的方式完全相同在“DetailCellView”笔尖中的“UITableViewCell”类型的第一个对象。因此,在这两种情况下你都会得到同样的细胞。 – omz

回答

2

您采取UITableViewCell类型的第一个对象在完全相同的方式加载小区1和小区2“ DetailCellView“笔尖。因此,在这两种情况下你都会得到同样的细胞。

0

好了,说实话我也不清楚,但是,负荷只调用apears是efficiant,当细胞是零,也许尝试加载第二个为别人在if(cell == nil)通话.. ,因为在代码的结尾,你再设置为零......所以也许:-)

+0

所以,我想念你的代码,在你想要拥有不同单元格的第一部分中,比在其他部分中......所以你称之为什么是不同的笔尖,因为你正在告诉加载笔尖,寻找“ DetailCellView“两次 – markus

相关问题