Ios UITableView中的两个不同UITableView单元格
我想创建一个具有2个剖面行的TableView。 此表有2个部分(第一部分有1个单元格,第二部分有3个单元格) 注意:第一节单元格与第二节单元格不同 这是我的代码,但不工作Ios UITableView中的两个不同UITableView单元格,ios,objective-c,uitableview,custom-cell,Ios,Objective C,Uitableview,Custom Cell,我想创建一个具有2个剖面行的TableView。 此表有2个部分(第一部分有1个单元格,第二部分有3个单元格) 注意:第一节单元格与第二节单元格不同 这是我的代码,但不工作 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *name = [_names objectForKey:key]; static
- (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
。
当我只运行此代码时,第一部分不工作,不显示单元格,但另一部分工作,请指导我并告诉我哪里是我的错误。尝试使用与第二个单元格相同的代码
if (indexPath.section == 0) {
FirstCell *cell = (FirstCell *)[self.table dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil)
{
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"FirstCell" owner:nil options:nil];
for (id currentObject in topLevelObject) {
if ([currentObject isKindOfClass:[FirstCell class]]) {
cell = (FirstCell *)currentObject;
break;
}
}
// Configure the cell...
cell.nameLable.text = [NSString stringWithFormat:@"blue"];
cell.numberLable.text = [NSString stringWithFormat:@"1212432543"];
cell.profileImage.image = [UIImage imageNamed: @"profile.png"];
return cell;
}
试试这个:
FirstCell *cell = (FirstCell *)[tableView dequeueReusableCellWithIdentifier:strEvalIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FirstCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
我认为问题就在这里:“cell=[[FirstCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];“您应该像加载第二节一样加载nib。@PoojaM.Bohora不需要加载nib,它可以是
UITableViewCell
@iphonec的自定义类:他在创建firstcell时没有使用其xib。Asper他的实现只是一个猜测。因为我还没有看到完整的代码。@PoojaM.Bohora与[[UITableViewCell alloc]init]不一样吗?其中UITableViewCell==FirstCell
?@mamal10:试试这个FirstCell*cell=(FirstCell*)[tableView dequeueReusableCellWithIdentifier:strevalidentier];如果(cell==nil){NSArray*nib=[[NSBundle mainBundle]loadNibNamed:@“FirstCell”所有者:self-options:nil];cell=[nib-objectAtIndex:0];}不要急于回答。if([currentObject isKindOfClass:[CustomerCell class]]应该是if([currentObject isKindOfClass:[FirstCell class]]进行了更改。谢谢。我的应用程序在这一行崩溃:NSArray*topLevelObject=[[NSBundle mainBundle]LoadNibName:@“FirstCell”所有者:无选项:无];是的,我的兄弟和我的应用程序在这一行崩溃:NSArray*topLevelObject=[[NSBundle mainBundle]loadNibNamed:@“FirstCell”所有者:nil选项:nil];@mamal10:您忽略了我的声明它的所有者本身?而不是nil。我的应用程序再次在这一行崩溃:NSArray*nib=[[NSBundle mainBundle]loadNibNamed:@“FirstCell”所有者:自我选项:nil];@mamal10崩溃的原因?我不知道崩溃的原因,但在控制台中打印此错误:libc++abi.dylib:以NSException(lldb)类型的未捕获异常终止。您能编辑您的帖子并粘贴所需的代码吗?请稍等几分钟,现在放入所有项目