Calling two different custom cell in one UITableView issue

14,623

You need to do something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                              reuseIdentifier:CellIdentifier];
        }

        containerObject = [objectCollection objectAtIndex:indexPath.row];
        [[cell textLabel] setText:containerObject.store];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    } else {
        static NSString* cellIdentifier1 = @"FeatureCell";

        FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
        if (cell1 == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil];
            cell1 = (FeatureCell*)[nib objectAtIndex:0];
        }

        cell1.img1.image = [UIImage imageNamed:@"shower.png"];
        cell1.img2.image = [UIImage imageNamed:@"parking.png"];

        return cell1;
    }
}

I might have the if condition backwards so double check that.

Share:
14,623
Luai Kalkatawi
Author by

Luai Kalkatawi

Learning Swift :)

Updated on June 27, 2022

Comments

  • Luai Kalkatawi
    Luai Kalkatawi almost 2 years

    I have created a custom cell FeatureCell which has 5 images in the row that will be called in the main view but when I call it I get empty row. So please where could be my problem?

    I have googled about custom cell and I used the way that I have to use in the code below but nothing happen.

    This is my FeatureCell.h

    @interface FeatureCell : UITableViewCell{
    
      IBOutlet UIImageView *img1;
      IBOutlet UIImageView *img2;
    }
    
    @property (nonatomic, retain) UIImageView *img1;
    @property (nonatomic, retain) UIImageView *img2;
    @end
    

    This is my FeatureCell.m

    @implementation FeatureCell
    
    @synthesize img,img1,img2,img3,img4;
    @end
    

    This is the my view-controller .m

    - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return [objectCollection count];
    }
    else{
        return 1;
       }
    }    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
    static NSString* cellIdentifier1 = @"FeatureCell";
    
    FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
    
    
    if (cell1 == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil];
    
        cell1 = (FeatureCell*)[nib objectAtIndex:0];
    }
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                      reuseIdentifier:CellIdentifier];
    
    }
    
    
    if ([indexPath section] == 0) {
    
        containerObject = [objectCollection objectAtIndex:indexPath.row];
    
        [[cell textLabel] setText:containerObject.store];
    
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    }
        else{
    
        cell1.img1.image = [UIImage imageNamed:@"shower.png"];
        cell1.img2.image = [UIImage imageNamed:@"parking.png"];
    
        }
            return cell;
    }