具有两个自定义单元(多个标识符)的UITableView

我试图把一个单元格作为每个单元格之间的空间 – 这将通过设置alpha = 0隐藏。在我的表中,空间单元格将是奇数的行。

请注意,实际的单元格高度是85,但隐藏的单元格高度(即单元格之间的空间)是20。

问题是空间单元格高度是85,但不是20,我不知道为什么。 也许单元格没有正确加载。

这里的CellUITableViewCell – 实际单元格,标识符为“单元格”。

Cell2是标识符为“Space”的空间。

上面的每个类都有自己的UITableViewCell类,并且XIB文件也被分配给它们中的每一个。 标识符也在IB中为每个Xib设置。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier1 = @"Cell"; static NSString *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; 

}

*我已经重新命名了一些NIB /类名,以便更好地理解。 *

首先,你应该注册每个单元的NIB:

 - (void)viewDidLoad{ [super viewDidLoad]; static NSString *CellIdentifier1 = @"ContentCell"; static NSString *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]; } 

因为你已经注册了NIB,所以dequeueReusableCellWithIdentifier:总会返回一个单元格:

 - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier1 = @"ContentCell"; static NSString *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; } } 

最后但并非最不重要的一点是,确保实现下面的委托方法:

 - (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 NSString *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

导入“MapTableViewCell.h”

 @implementation MapTableViewCell @synthesize nearbyLocation; -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)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 

使用默认单元格数量的自定义单元格的最佳方法

除了提供的答案,我想强调每个不同的自定义单元单元标识符也必须不同

例如标识符为"Cell"自定义"Cell" cellB和标识符为"Cell2"自定义"Cell" "Cell2"