2010-08-06 71 views
0

所以我有一个自定义tableviewcells设置编程。我有4类自定义单元格,一个部分有一个自定义单元格。但我不知道这是错误的或不:iPhone - 以编程方式创建自定义TableView

(UITableViewCell *)tableView:(UITableView *)TheTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


    static NSString *ShopsIdentifier = @"ShopsIdentifier"; 
    static NSString *DescriptionsIdentifier = @"DescriptionsIdentifier"; 
    static NSString *ServicesIdentifier = @"ServicesIdentifier"; 
    static NSString *PartnersIdentifier = @"PartnersIdentifier"; 


    if (indexPath.section == kShops) { 
     NSLog(@"Chargement cellule ShopDetailCell"); 
     ShopDetailCell * shopshopCell = (ShopDetailCell *)[tableView dequeueReusableCellWithIdentifier:ShopsIdentifier]; 
     if (shopCell == nil) { 
      shopCell = [[[ShopDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ShopsIdentifier] autorelease]; 
     } 
     shopCell.detailController = self; 
     shopCell.shop = self.shop; 
     return shopCell; 

    } 

    if (indexPath.section == kDescriptions) { 
     NSLog(@"Chargement cellule DescriptionCell"); 
     DescriptionCell * descriptionCell = (DescriptionCell *)[tableView dequeueReusableCellWithIdentifier:DescriptionsIdentifier]; 
     if (descriptionCell == nil) { 
      descriptionCell = [[[DescriptionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DescriptionsIdentifier] autorelease]; 
     } 
     descriptionCell.shop = self.shop; 
     return descriptionCell; 

    } 
    if (indexPath.section == kServices) { 
     NSLog(@"Chargement cellule ServicesCell"); 
     ServicesCell * servicesCell = (ServicesCell *)[tableView dequeueReusableCellWithIdentifier:ServicesIdentifier]; 
     if (servicesCell == nil) { 
      servicesCell = [[[ServicesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ServicesIdentifier] autorelease]; 
     } 
     servicesCell.shop = self.shop; 
     return servicesCell; 
    } 

    if (indexPath.section == kPartners) { 
     PartnersCell * partnersCell = (PartnersCell *)[tableView dequeueReusableCellWithIdentifier:PartnersIdentifier]; 
     if (partnersCell == nil) { 
      partnersCell = [[[PartnersCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PartnersIdentifier] autorelease]; 
     } 
     NSMutableDictionary * aPartner = [[NSMutableDictionary alloc] init]; 
     aPartner = [shop.partners objectAtIndex:indexPath.row]; 
     partnersCell.partner = aPartner; 
     [aPartner release]; 
     return partnersCell; 
    } 

    return nil; 
} 
+0

你可能会失去了'static'关键字。但它的工作?因为它看起来不错。 – mvds 2010-08-06 14:00:14

+0

是的,它的工作。但是,当我想要将细胞排出或创建新细胞时,它会失败。 – 2010-08-09 07:50:17

回答

0

如果你在同一个NIB文件作为控制器创建这些定制单元,然后我发现最容易做的事情是建立对它们的引用(宣布他们)并为他们创造一个渠道。由于您只有一个自定义单元格的实例,因此您不希望将单元格出列或创建新单元格。 (如果需要定制单元格的更多实例,则必须在单独的nib文件中创建它们并使用队列机制。)

在.h文件中: UITableViewCell IBOutlet * shopDetailCell;

然后在的cellForRowAtIndexPath你可以:

UITableView *cell; 

if (indexPath.row == kShopCellRow) { 
    cell = self.shopDetailCell; 
    cell.shop = self.shop; // etc. whatever initialization you have to do 
    return cell;   // do this at the end of your if statements. 
} 
+0

是的,但我不使用NIB文件。我以编程方式设置,没有IB。 – 2010-08-09 07:40:26

+0

同样的想法成立,你应该有一个参考单元格。队列机制适用于您使用大量相同类型的单元格时,以便系统可以决定是应该重新使用还是重新创建它们。 – joelm 2010-08-10 14:44:07

1

这段代码是错误的:

NSMutableDictionary * aPartner = [[NSMutableDictionary alloc] init]; 
aPartner = [shop.partners objectAtIndex:indexPath.row]; 
partnersCell.partner = aPartner; 
[aPartner release]; 

极品:

partnersCell.partner = [shop.partners objectAtIndex:indexPath.row]; 
+0

是的,我忘了它。 – 2010-08-09 07:45:23

相关问题