Ios 为UITableView中的每个部分使用不同的自定义单元格
我发现桌子上有些奇怪的东西。我想创建一个包含两个或多个部分的表,在第一个部分,我想和其他部分一起使用不同的自定义单元格 所以我在我的Ios 为UITableView中的每个部分使用不同的自定义单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我发现桌子上有些奇怪的东西。我想创建一个包含两个或多个部分的表,在第一个部分,我想和其他部分一起使用不同的自定义单元格 所以我在我的tableView:cellforrowatinexpath - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"cell";
tableView:cellforrowatinexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
if (indexPath.section == 0) {
// cell for section one
HeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!headerCell) {
[tableView registerNib:[UINib nibWithNibName:@"HeaderCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
headerCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
headerCell.labelName.text = @"First Section";
return headerCell;
}
else {
// Cell for another section
DetailCell *detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!detailSection) {
[tableView registerNib:[UINib nibWithNibName:@"DetailCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
detailCell.textLabel.text = @"Another Section Row";
return detailCell;
}
}
在第一部分中,我想对我的行使用headerCell
,然后对其他行使用detailCell
。此代码有效,但在第二节的行上,似乎仍在“detailCell
下使用headerCell
”。我将标签添加到headerCell.xib
,它仍然显示在detailCell
上。看这个
我认为所有这些都是因为我对所有部分使用了一个单元格标识符。有人有解决办法吗?非常感谢。每种类型的自定义单元格都应该有自己的唯一标识符。您的代码试图对所有单元格使用相同的单元格标识符。那不行 另外,在
viewDidLoad
中注册这两种单元格类型,而不是cellforrowatinexpath:
试试这个:
static NSString *cellIdentifier0 = @"cell0";
static NSString *cellIdentifier1 = @"cell1";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
// cell for section one
HeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier0 forIndexPath:indexPath];
headerCell.labelName.text = @"First Section";
return headerCell;
} else {
// Cell for another section
DetailCell *detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1 forIndexPath:indexPath];
detailCell.textLabel.text = @"Another Section Row";
return detailCell;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// the rest of your code
[self.tableView registerNib:[UINib nibWithNibName:@"HeaderCell" bundle:nil] forCellReuseIdentifier:cellIdentifier0];
[self.tableView registerNib:[UINib nibWithNibName:@"DetailCell" bundle:nil] forCellReuseIdentifier:cellIdentifier1];
}
是的,您需要使用两种不同的手机识别码。非常感谢您,rmaddy!