如何在Ipad上的TableView中加载自定义TableViewCell?

如何在Ipad上的TableView中加载自定义TableViewCell?,ipad,uitableview,custom-cell,Ipad,Uitableview,Custom Cell,首先,对不起我的英语,我是法国人:) 正如标题中所说,我想在ipad上的简单tableview中放置一个自定义tableviewcell。这是必要的,因为我想生成一个有5个标签的表 我使用一个单独的带有NIB的UITableViewCell类 这是我的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static N

首先,对不起我的英语,我是法国人:)

正如标题中所说,我想在ipad上的简单tableview中放置一个自定义tableviewcell。这是必要的,因为我想生成一个有5个标签的表

我使用一个单独的带有NIB的
UITableViewCell

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    static NSString *CellIdentifier = @"CellIdentifier";

    CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];    

    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil]; 
        cell = customTableCell;
        self.customTableCell = nil;
    }   

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [self configureCell:cell atIndexPath:indexPath];

    NSString *rowLabelEcheance = [[NSString alloc] initWithFormat:@"%@", [contentsList objectAtIndex:indexPath.row]];
    [customTableCell.labelEcheance setText:rowLabelEcheance];

    NSString *rowLabelCapitalRestantDu = [[NSString alloc] initWithFormat:@"%@", >>>>>>>  [contentsList2 objectAtIndex:indexPath.row]];
    [customTableCell.labelCapitalRestantDu setText:rowLabelCapitalRestantDu];

    NSString *rowLabelInterets = [[NSString alloc] initWithFormat:@"%@", [contentsList3 objectAtIndex:indexPath.row]];
    [customTableCell.labelInterets setText:rowLabelInterets];

    NSString *rowLabelAmortissement = [[NSString alloc] initWithFormat:@"%@", >[contentsList4 objectAtIndex:indexPath.row]];
    [customTableCell.labelAmortissement setText:rowLabelAmortissement];

    NSString *rowLabelMontant = [[NSString alloc] initWithFormat:@"%@", [contentsList5 objectAtIndex:indexPath.row]];
    [customTableCell.labelMontant setText:rowLabelMontant];

    return cell;
}
你能帮我吗?我花了8个小时来解决这个问题


谢谢大家!

如果其他人有此问题,您可以在此处下载一个工作示例项目:

否则,以下是说明:

我知道有几种方法可以做到这一点

假设以下名称和文件结构

-UITableCellCustomClass.h
-UITableCellCustomClass.m
-UITableCellCustomClass.xib
-UITableViewClass.h 
-UITableViewCustomClass.m
-UITableViewCustomClass.xib 
使用Interface Builder,要创建自定义表格单元格,需要执行以下操作:

1.)从从UITableViewCell继承的NIB(如您所做)创建自定义表格单元格

2.)在IB中为自定义表格单元格类取消勾选“文件所有者”,并将其指向希望单元格显示在其中的表格类,例如,将其更改为UITableViewCustomClass

3.)单击IB屏幕中的表格单元格,转到显示UITableViewCell的字段,并将其更改为从自定义表格单元格类继承,例如UITableCellCustomClass

4.)确保已为textfields创建了所有字段和属性以及输出,并在interfacebuilder中的表格单元格类和自定义单元格xib中的单元格之间建立了必要的连接(注意:不是文件的所有者…)

5.)我看到您在那里有“cell identifier”变量……但如果您还没有,请确保在CustomTableCell xib中更改了cell identifier字段以匹配您的自定义标识符

如果我的记忆正确的话,我相信这应该可以做到。看起来您正在对表执行其余操作,并在单元格上设置属性

我相信这就是全部。我在某个地方把它写得很详细,因为它太痛苦了

更新:

如果有五个标签,则可能需要更改表行的垂直高度,以便显示所有标签(除非有非常宽的表)

确保表的委托和数据源都指向其父类(承载单元加载逻辑的父类)

必须重写此函数:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

        return YOURCUSTOMROWHEIGHT;
}
更新:显然上述内容还不够,因此我将重试:

尝试使用以下UINib方法执行此操作:

将其添加到UICustomTableView.h类中

UINib *cellLoader;
下一步,尝试以下步骤(假设您遵循了上述所有步骤)…也许UINib加载程序会有所帮助?我很确定我在看了笔记之后,已经涵盖了所有正确的步骤。要尝试的一件事是创建一个扩展UIViewController的类,然后从中删除视图,添加表单元格并执行上述步骤,然后将基类更改为从UITableCellView继承

static NSString *CellClassName = @"YourCellClassName";

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) 
    {
        cellLoader = [[UINib nibWithNibName:CellClassName bundle:[NSBundle mainBundle]] retain];
    }
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Make sure your identifier is set properly in IB
    CustomCellClass *cell = (CustomCellClass *)[tableView     dequeueReusableCellWithIdentifier:CellClassName];
    if (!cell)
    {
        NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];
    }
     /* set your cell properties here */



    return cell;
}

使用以下简单代码:

...
if(cell == nil){
   NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCellView" owner:self options:nil];
   cell = (MyCellView *) [nib objectAtIndex:0];
}
...

我已经完成了你说的所有要点。控制台说:“2011-08-10 09:35:06.508 EmpruntClassiqueV10[43282:207]-[SecondViewController配置单元:atIndexPath:]:未识别的选择器发送到实例0x4b66700”…这是ipad,我有空间在一行中放置5个标签。我把它们放在我的自定义单元格笔尖的IB中。