views:

160

answers:

2

Hi,

So I've got a custom tableviewcells set up programmatically. I have 4 classes of custom cells, one custom cells for one section. But i don't know if it's wrong or not :

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

}

Thanks in advance.

A: 

If you have created these custom cells in the same NIB file as the controller, then I find the easiest thing to do is create a reference to them (declare them) and create an outlet to them. You don't want to dequeue cells or create new ones since you have one and only one instance of your custom cell. (If you need more instances of your custom cell, then you have to create them in a separate nib file and use the queue mechanism.)

in the .h file: UITableViewCell IBOutlet *shopDetailCell;

Then in cellForRowAtIndexPath you can just:

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.
 }
joelm
Yes, but i don't use NIB file. I set up programmatically, without IB.
ALCARAZ François-Julien
The same idea holds, you should have a reference to the cells. The queue mechanism is for when you use a lot of the same type of cells so that the system can decide if they should be reused or recreated.
joelm
A: 

This code is wrong:

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

Need:

partnersCell.partner = [shop.partners objectAtIndex:indexPath.row];
jamapag
Yes, i forgot it.
ALCARAZ François-Julien