1

嗨,我尝试使用 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 个对象!!!

4

4 回答 4

1

您应该确认您对 dequeueReusable... 的每个调用实际上都返回了一个单元格。利用:

NSAssert (cell, @"Missed Cell");

在每个“if”语句的正文中。我怀疑正在返回一个零单元格。这将是因为您的“ProfilCell”、“ProfilCell1”、...的重用标识符与情节提要中的单元格标识符不一致。要修复它,请转到情节提要,逐个选择每个原型单元,确认“标识符”与您的代码中的内容一致。如果不是,则更改为同意。

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

于 2012-06-06T03:41:16.880 回答
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;

    }

我在这里所做的是为 type 的单元格设置一个 var id,这意味着它可以是任何东西。在您的 if 语句中,您决定什么是单元格,并相应地进行设置。最后只返回 1 个单元格实例,而不是在每个语句中。希望这可以帮助

于 2012-06-06T03:11:52.673 回答
0

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

还要检查您的单元格标识符是否与情节提要中的标识符匹配。

于 2012-06-06T03:53:20.167 回答
-2

调用后,dequeueReusableCellWithIdentifier:必须检查返回的值nil。如果返回的值不是nil,那么你很好;你从队列中得到了一个可重复使用的单元格。 但如果返回值为nil,则队列中没有可用的可重用单元。(对于新创建的表,情况总是如此。)在后一种情况下,您必须创建一个新单元格,或者以编程方式使用allocand init

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                               reuseIdentifier:@"TableViewCell"] autorelease];

或通过从 .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;
}
于 2012-06-06T03:27:39.280 回答