无法在一个视图控制器中加载两个表视图

时间:2015-06-12 10:09:10

标签: ios objective-c uitableview

错误: - 使用未声明的标识符单元格。

无法在一个视图控制器中加载两个自定义单元格。

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (tableView == tableView_grantRecordAccess)
    {
        UITableViewCell *cell = [tableView_grantRecordAccess dequeueReusableCellWithIdentifier:@"EHSRecordAccessGrantCell"];

           if (cell == nil) {

               // Load the top-level objects from the custom cell XIB.

                NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EHSRecordAccessGrantCell" owner:self options:nil];

                // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).

                cell = [topLevelObjects objectAtIndex:0];

            }

        return cell;

    }

 else if (tableView == tableView_accessRecordRequest)  {

        UITableViewCell *cell = [tableView_accessRecordRequest dequeueReusableCellWithIdentifier:@"EHSAccessRecordCell"];

        if (cell == nil) {

            // Load the top-level objects from the custom cell XIB.

            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EHSAccessRecordCell" owner:self options:nil];

            // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).

            cell = [topLevelObjects objectAtIndex:0];
        }
            }

    return cell;

}

4 个答案:

答案 0 :(得分:1)

这是单元格引用的范围问题,如果您将在方法的第一行引用单元格,然后仅在两种条件下使用该引用,那么它将起作用。

答案 1 :(得分:1)

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
      UITableViewCell *cell = nil;
      if (tableView == tableView_grantRecordAccess)
      {
         cell = [tableView_grantRecordAccess dequeueReusableCellWithIdentifier:@"EHSRecordAccessGrantCell"];

         if (cell == nil) {

           // Load the top-level objects from the custom cell XIB.

            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EHSRecordAccessGrantCell" owner:self options:nil];

            // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).

            cell = [topLevelObjects objectAtIndex:0];

        }

      return cell;

}
else if (tableView == tableView_accessRecordRequest)  {

    cell = [tableView_accessRecordRequest dequeueReusableCellWithIdentifier:@"EHSAccessRecordCell"];

    if (cell == nil) {

        // Load the top-level objects from the custom cell XIB.

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EHSAccessRecordCell" owner:self options:nil];

        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).

        cell = [topLevelObjects objectAtIndex:0];
    }
  }
  return cell;

}

答案 2 :(得分:1)

如果条件分别在上面添加这一行 // if(cell == nil){... }

//对于第一个表条件

if (![cell isKindOfClass:[EHSRecordAccessGrantCell
 class]])cell = nil;

//对于第二个表条件

if (![cell isKindOfClass:[EHSAccessRecordCell
 class]])cell = nil;

崩溃,因为你已经注册了两个单元格到表格,无论何时第二个单元格已经有第二个单元格的引用,这是不同类型的

答案 3 :(得分:0)

在故事板中检查正确的小区标识符。

在身份检查器中选择表格的原型单元格标识符。

查看您的错误陈述,我可以假设 - 您可能忘记将标识符分配给您的单元格,或者您在代码中使用了错误的标识符(可能是拼写错误)(cellForRowAtIndexPath方法)。