UITableView avec deux cellules personnalisées (plusieurs identifiants)

J'essaye de placer une cellule comme un espace entre chaque cellule – qui sera caché en plaçant alpha = 0. Dans ma table, les cellules spatiales seront pour les rangées qui sont étranges.

Notez que la hauteur réelle de la cellule est de 85, mais la hauteur de la cellule cachée (c'est-à-dire l'espace entre les cellules) est de 20.

Le problème est que la taille de la cellule spatiale est de 85, mais pas de 20, je ne sais pas pourquoi. Peut-être que la cellule n'est pas chargée correctement.

Cell ici est UITableViewCell – la cellule réelle – avec l'identifiant 'Cell'.

Cell2 est l'espace avec l'identifiant 'Space'.

Chaque class ci-dessus a sa propre class UITableViewCell et les files XIB sont également affectés à chacun d'eux. L' identifiant est également défini dans le IB pour chaque Xib.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSSsortingng *CellIdentifier1 = @"Cell"; static NSSsortingng *CellIdentifier2 = @"Space"; Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; if(!cell) { NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:nil options:nil]; for (id obj in ar) { if ([obj isKindOfClass:[Cell class]]) { cell = (Cell *)obj; break; } } } if (indexPath.row % 2 == 1) { Cell2 *cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; if (!cell2) { NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"Cell2" owner:nil options:nil]; for(id obj in ar) { if([obj isKindOfClass:[Cell2 class]]) { cell2 = (Cell2 *)obj; break; } } // Method 1 cell2 = [[Cell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]; // Method 2 //cell2 = [[Cell2 alloc] init]; // Method 3 //cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; [cell2.contentView setAlpha:0]; // prevent selection and other stuff [cell2 setUserInteractionEnabled:NO]; } return cell2; } else { // Configure the actual cell } return cell; 

}

* J'ai renommé certains de vos noms NIB / Class pour une meilleure compréhension. *

Tout d'abord, vous devez save la NIB de chaque cellule:

 - (void)viewDidLoad{ [super viewDidLoad]; static NSSsortingng *CellIdentifier1 = @"ContentCell"; static NSSsortingng *CellIdentifier2 = @"SpaceCell"; UINib *nib = [UINib nibWithNibName:@"CellViewNIBName" bundle:nil]; [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier1]; nib = [UINib nibWithNibName:@"CellSpaceNIBName" bundle:nil]; [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier2]; self.contentView.hidden = YES; [self loadData]; } 

Puisque vous avez enregistré les dequeueReusableCellWithIdentifier: , dequeueReusableCellWithIdentifier: returnnera toujours une cellule:

 - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSSsortingng *CellIdentifier1 = @"ContentCell"; static NSSsortingng *CellIdentifier2 = @"SpaceCell"; // Space Cell if (indexPath.row % 2 == 1) { CellSpace *cell = (CellSpace *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; return cell; } // Content cell else { CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; // Configure cell return cell; } } 

Enfin, assurez-vous d'implémenter la méthode de délégation suivante:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // Space cell's height if (indexPath.row % 2 == 1) { return 20.0f; } // Content cell's height else { return 80.0f; } } 
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *returncell; static NSSsortingng *cellIdentifier ; if(indexPath.section == 0) { cellIdentifier = @"cell1"; } else if (indexPath.section == 1) { cellIdentifier = @"cell2"; } UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; MapTableViewCell *myCustomCell = (MapTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if(indexPath.section == 0) { if(myCell == nil) { myCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; getLocationBtn = [UIButton buttonWithType:UIButtonTypeCustom]; getLocationBtn.frame = CGRectMake(myCell.frame.origin.x,myCell.frame.origin.y+5 , 200, 30); [getLocationBtn setTitle:@"your button title" forState:UIControlStateNormal]; [getLocationBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; [getLocationBtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside]; } [myCell.contentView addSubview:getLocationBtn]; returncell = myCell; } else if (indexPath.section == 1) { if (myCustomCell == nil) { myCustomCell = [[MapTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } myCustomCell.nearbyLocation.text = @"demo Text"; returncell = myCustomCell; } return returncell; } 

// mycustom tablviewcell

importer "MapTableViewCell.h"

 @implementation MapTableViewCell @synthesize nearbyLocation; -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSSsortingng *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if(self) { self.backgroundColor = [UIColor groupTableViewBackgroundColor]; nearbyLocation = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 200, 30)]; [self addSubview:nearbyLocation]; } return self; } @end 

La meilleure façon d'utiliser le nombre de cellules personnalisées avec la cellule par défaut

En outre pour les réponses fournies, je tiens à souligner sur l' identificateur de cellule pour chaque cellule personnalisée doit être différente aussi .

Par exemple cellA personnalisée avec l'identifiant "Cell" et la cellB personnalisée avec l'identifiant "Cell2" .