2015-06-12 70 views
-1

错误: - 使用未声明的标识符单元格。无法在一个视图控制器中加载两个表视图

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

-(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; 

} 
+0

检查XIB中的单元格标识符。 –

+0

其他条件,如果我们得到错误使用未声明的标识符单元格 –

+0

当您返回单元格时,在else else条件中,右括号错位(“}”),因此未定义。正确缩进您的代码,您会看到。它应该在'return cell;'之后而不是之前。 – Larme

回答

1

这是单元格引用范围的问题,如果你将在方法的第一线使细胞的参考,然后使用该参考之用在这两个条件,那么它会工作。

+0

但第一个条件是执行else如果条件不执行,我想一次加载两个表视图帮我 –

0

检查您的故事板是否有正确的单元标识符。

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

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

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; 

} 
+0

我们无法加载第二个表视图否则条件不执行。我想一次加载两个表视图。 –

1

添加此线之上如果条件分别 //如果(细胞==无){...}

//对于第一表条件

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

//对于第二表条件

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

因为您已经注册了两个单元格表格,并且每当涉及到第二个单元格时,该单元格都具有第二个不同类型单元格的引用。

+0

我们得到一个错误使用未声明的EHSAccessRecordCell –

相关问题