UITableView 中两个不同的 UITableViewCell [英] Two different UITableViewCell in UITableView
问题描述
我想创建一个包含 2 个部分行的 TableView.此表有 2 个部分(第一部分有 1 个单元格,第二部分有 3 个单元格)
I want to create one TableView that has 2 section rows. this table has 2 section (first section has 1 cell and second section has 3 cell)
注意:第一节的单元格与第二节的单元格不同.
notice:cell of first section different with cells of second section.
这是我的代码,但不起作用!!!
this is my code but don't working!!!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *name = [_names objectForKey:key];
static NSString *CellIdentifier2 = @"CustomerCell";
if (indexPath.section == 0) {
static NSString *CellIdentifier = @"myCell";
FirstCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[FirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.nameLable.text = [NSString stringWithFormat:@"blue"];
cell.numberLable.text = [NSString stringWithFormat:@"1212432543"];
cell.profileImage.image = [UIImage imageNamed: @"profile.png"];
return cell;
//this part not working !!! XD
}
else if (indexPath.section >= 1) {
CustomerCell *cell = (CustomerCell *)[self.table dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil)
{
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"CustomerCell" owner:nil options:nil];
for (id currentObject in topLevelObject) {
if ([currentObject isKindOfClass:[CustomerCell class]]) {
cell = (CustomerCell *)currentObject;
break;
}
}
}
// Configure the cell...
cell.titleLable.text = [name objectAtIndex:indexPath.row];
return cell;
}
return nil;
}
customerCell &FirstCell 是用于自定义单元格的两个 UITableViewCell
.当我只运行这段代码时,第一个部分不起作用并且不显示单元格但另一部分正在工作,请指导我并告诉我我的错误在哪里.
customerCell & FirstCell are tow UITableViewCell
for custom cells.
when I to do run this code only section one not working and don't show cell but another section is working please guide me and tell me where is it my mistake.
推荐答案
试试这个:
FirstCell *cell = (FirstCell *)[tableView dequeueReusableCellWithIdentifier:strEvalIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FirstCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
这篇关于UITableView 中两个不同的 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!