2011-08-08 48 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

相關問題