开发者

iPhone - Creating a custom TableView programmatically

开发者 https://www.devze.com 2023-01-10 18:04 出处:网络
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 :

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:Shop开发者_运维技巧sIdentifier] 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;
}


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


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.
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消