Calling two different custom cell in one UITableView issue

℡╲_俬逩灬. 提交于 2019-12-19 06:57:21

问题


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

回答1:


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.




回答2:


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.




回答3:


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

}


来源:https://stackoverflow.com/questions/17711920/calling-two-different-custom-cell-in-one-uitableview-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!