iPhone - 以编程方式创建自定义TableView

时间:2010-08-06 13:55:27

标签: iphone uitableview

所以我有一个以编程方式设置的自定义tableviewcells。我有4类自定义单元格,一个自定义单元格。但我不知道这是不是错了:

(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;
}

2 个答案:

答案 0 :(得分:1)

此代码错误:

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

需要:

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

答案 1 :(得分: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.
 }