Calling two different custom cells in the same UITableView problem

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

I have googled about a custom cell, and I used what I need to use in the code below, but nothing happens.

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 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; } 
+6
source share
3 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 may have an if condition back, so double check this.

+13
source

This implementation:

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

Results in a cell with some access methods, but not initialized instances. You need to implement the init method to create instances or bind the variables (which you define as output) to the corresponding XIB file. Read this guide .

I also agree with the comments about the structure of how you create your different cell instances. This is very disorganized and you seem to be mixing links. You must separate things into different methods, or at least put code to create cells inside if so that you cannot seal cell instance names.

0
source
 - (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; } 
0
source

Source: https://habr.com/ru/post/949693/


All Articles