Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 312
Luai Kalkatawi Avatar asked Jul 17 '13 23:07

Luai Kalkatawi


People also ask

Is it possible to add UITableView within a UITableViewCell?

Implementation of adding a table view inside the cell of UItableview aka, Nested Table View. Used the Power of Autosizing table view and delegate to achieve the expansion and collapse of the cell height.

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.

How do I deselect a selected UITableView cell?

How to deselect a UITableViewCell using clearsSelectionOnViewWillAppear. If you set this property to be true the user's selected cell will automatically be deselected when they return to the table view.


1 Answers

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.

like image 113
rmaddy Avatar answered Sep 22 '22 23:09

rmaddy