简体   繁体   中英

Calling two different custom cell in one UITableView issue

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

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.

This implementation:

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

Results in a cell with some accessor methods but no initialised instances. You need to implement an init method to create the instances or connect the variables (which you define as outlets) to the associated XIB file. Read this guide .

I also agree with the comments about the structure of how you're creating your different cell instances. It's very disorganised and you seem to be mixing up references. You should split things out into different methods or at least put the code for creating the cells inside the if statements so you can't typo the cell instance names.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{ if (indexPath.row == 0) //not indexPath.section...fixed my problem

{
   static NSString *CellIdentifier = @"MenuHeadingCustomCell";

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

   return cell;
}





else
   {
       static NSString* cellIdentifier1 = @"LevelViewCell";
       LevelViewCell *cell1 =[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
       if (cell1 ==nil)
       {
           cell1 = [[LevelViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellIdentifier1];

       // row counts which dictionary entry to load:
         _Dictionary=[_array objectAtIndex:indexPath.row];

       cell1.LevelTitleText.text =[NSString stringWithFormat:@"%@",[_Dictionary objectForKey:@"LevelLabelText"]];
       cell1.LevelSubText.text =[NSString stringWithFormat:@"%@",[_Dictionary objectForKey:@"LevelLabelSubText"]];
       cell1.LevelThumb.image =[UIImage imageNamed:[_Dictionary objectForKey:@"LevelLabelThumb"]];

   }
   return cell1;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM