Iphone 原型单元uitableview

Iphone 原型单元uitableview,iphone,xcode,uitableview,Iphone,Xcode,Uitableview,嗨,我试着在故事板上使用4个原型单元。对于每个原型单元格,我都有一个带有标签的uitableviewCell类 看看我的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"ProfilCell"; static NSString *Cell

嗨,我试着在故事板上使用4个原型单元。对于每个原型单元格,我都有一个带有标签的uitableviewCell类

看看我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *CellIdentifier  = @"ProfilCell";
    static NSString *CellIdentifier1 = @"ProfilCell1";
    static NSString *CellIdentifier2 = @"ProfilCell2";
    static NSString *CellIdentifier3 = @"ProfilCell3";
    static NSString *CellIdentifier4 = @"ProfilCell4";

    if(indexPath.section == 0) 
    {
        ProfilCell* cell =(ProfilCell*)  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        cell.pseudoLabel.text=[[infoArray objectAtIndex:indexPath.row]valueForKey:@"pseudo"];

        return cell;

    }

      if(indexPath.section == 1) 
      {
        ProfilCell2* cell = (ProfilCell2*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

       cell.textLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"status"];

        return cell;

    }

   if(indexPath.section == 2) 
    {
        switch (indexPath.row) 
        {
            case 0:
            {

                 ProfilCell3* cell =(ProfilCell3*)  [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
                 cell.ageLabel.text=  [[infoArray objectAtIndex:indexPath.row]valueForKey:@"age"];
              return cell;
            }
                break;

            case 1:

            {

                ProfilCell4* cell = (ProfilCell4*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
                cell.deptLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"localite"];

                return cell;

            }
                break;
            case 2:
            {
                ProfilCell5* cell = (ProfilCell5*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier4];


                cell.membreLab.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"membre"];

                return cell;

            }
                break;   
            default:
                break;
        }
    }
          return nil;
}
在第0节第1节中,我的标签中有数据,但在第2节案例0中,我有:

[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
为什么我的数组看起来是空的


现在可以了,只需为indexPath指定0,因为数组中只有1个对象

你可能应该试试这样的东西

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"ProfilCell";
    static NSString *CellIdentifier1 = @"ProfilCell1";
    static NSString *CellIdentifier2 = @"ProfilCell2";
    static NSString *CellIdentifier3 = @"ProfilCell3";

    id cell = nil;

    if(indexPath.section == 0) {
              cell = (ProfilCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
              cell.pseudoLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"pseudo"];
    }

    if(indexPath.section == 1) {
            cell = (ProfilCell2*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
            cell.statusLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"status"];
    }

    if(indexPath.section == 2) {
            cell =(ProfilCell3*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
            cell.ageLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"age"];
    }

    if(indexPath.section == 3) {
            cell = (ProfilCell4*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
            cell.deptLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"localite"];
    }


    return cell;

    }
我在这里所做的是为id类型的单元格设置一个var,这意味着它可以是任何东西。在if语句中,您可以决定单元格是什么,并相应地进行设置。最后只返回一个单元格实例,而不是在每个语句中。希望这对调用dequeueReusableCellWithIdentifier后有所帮助:您必须检查返回值是否为nil。如果返回值不是nil,则可以;队列中有一个可重复使用的单元格。但如果返回值为nil,则队列中没有可用的可重用单元。新创建的表总是这样。在后一种情况下,您必须使用alloc和init以编程方式创建一个新单元:

或者从.xib文件中加载:

cell = [[[NSBundle mainBundle] loadNibNamed:@"TableViewCell"
                                      owner:self options:nil] lastObject];
在您的情况下,您可以在其他部分重复以下操作:

if (indexPath.section == 0)
{
    ProfilCell *cell = (ProfilCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)  // if we did not get a reusable cell from the queue, we make one…
    {
        cell = [[ProfilCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.pseudoLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"pseudo"];
    return cell;
}

您应该确认您的每个出列调用。。。实际上是返回一个单元格。使用:

NSAssert (cell, @"Missed Cell");
在每个“如果”语句的正文中。我的怀疑是一个零号牢房正在被归还。这是因为您的ProfilCell重用标识符ProfilCell1。。。与情节提要中的单元标识符不一致。要修复它,请转到情节提要,逐个选择每个原型单元,确认“标识符”与代码中的内容一致。如果没有,请将其更改为“同意”


“标识符”必须匹配;不是“标签”

检查ProfilCell、ProfilCell2、ProfilCell3和ProfilCell4是否都是UITableViewCell的子类


还要检查您的单元格标识符是否与故事板中的匹配。

如果没有“如果”语句,“返回单元格”将返回nil-这与他最初的代码发布有什么不同?您如何知道是否没有硬编码的行数,而且永远不会有更多或更少?。您好,我使用了您的方法,但它也会因以下错误而崩溃:**由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSIndexPath setTableViewStyle::]:将无法识别的选择器发送到实例使用带有原型单元格的情节提要时,dequeueReusableCellWithIdentifier始终返回有效的单元格。没有必要显式地alloc/init one。这不是一条与您以前收到的错误消息完全不同的错误消息吗?这意味着您修复了以前的问题,但又转到了一个新的错误。您是谁?您是对的?另外,为什么要将setTableViewStyle:method/message发送到NSIndexPath的实例?前面的错误消息“UITableView数据源必须从tableView返回一个单元格:CellForRowatingIndexPath:”发生了什么?你做了什么使错误信息消失了?
NSAssert (cell, @"Missed Cell");