简体   繁体   English

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

[英]unable to load two table views in one view controller

Error:- use of un declared identifier cell. 错误:-使用未声明的标识符单元格。

unable to load two custom cells in one view controller. 无法在一个视图控制器中加载两个自定义单元格。

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

}

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

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

}

add this line above if condition respectively //if (cell == nil) { ... } 在if条件分别上分别添加此行// // if(cell == nil){...}

//For first table condition //对于第一个表条件

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

//For second table condition //对于第二个表条件

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

crash because of you have register both cell to table and whenever it comes to second one the cell already have the reference of the second one cell that is differet typeof 由于您已将两个单元格都注册到表而崩溃,并且每当涉及到第二个单元格时,该单元格就已经具有第二个单元格的引用,该第二个单元格是不同类型

Check in your storyboard for the right cell identifier. 在情节提要中检查正确的单元格标识符。

Select both of your table's prototype cell's identifier in identity inspector. 在身份检查器中选择表的原型单元格的标识符。

Looking at your error statement, I can assume- You may have forgotten to assign the identifier to your cell or you are using wrong identifier(may be a spelling mistake) in your code(cellForRowAtIndexPath method). 查看您的错误声明,我可以假设-您可能忘记了为该单元格分配标识符,或者您在代码(cellForRowAtIndexPath方法)中使用了错误的标识符(可能是拼写错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一个视图控制器中将xib加载到两个容器视图中 - How to load xib into two Container Views within one view controller Swift 2.0在一个视图控制器cellForRowIndexPath中包含两个表视图 - Swift 2.0 two table views in one view controller cellForRowIndexPath 视图控制器中的两个表视图:Swift - Two table views in view controller: Swift 如何使用两个自定义 UITableViewCell 在一个视图控制器中创建两个表视图? - How do I create two table views in one view controller with two custom UITableViewCells? 一个视图控制器中具有不同重用单元格的两个集合视图 - Two Collection Views with Different Reuse Cells in One View Controller Swift 4 One View Controller两个视图隐藏导航栏 - Swift 4 One View Controller two views hide navigation bar 一个视图控制器中的两个警报视图 - buttonIndex响应 - Two Alert Views in One View Controller - buttonIndex Response 一个视图控制器可以处理多少个子视图和表视图 - How many subviews and table views can one view controller handle 不同视图控制器中的两个表视图一个工作一个不工作(Swift 4) - Two Table Views In Different View Controllers one working and one not(Swift 4) 一个视图控制器中有两个表视图,一个表视图未显示 - Two tableviews in one view controller, one table view not showing up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM