【问题标题】:My app crash, reason 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath我的应用程序崩溃,原因'UITableView dataSource 必须从 tableView 返回一个单元格:cellForRowAtIndexPath
【发布时间】:2015-06-08 11:45:40
【问题描述】:

我正在使用 2 个表格视图。我正在使用标签使用这两个表。在父 TableView 中有 3 个单元格。在父表的第三个单元格中添加了另一个表。我仔细检查了整个代码,但找不到错误。但是我收到这个错误,我很困惑为什么?我不知道问题出在哪里。我的应用程序崩溃了。这是我的崩溃代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {   
        UITableViewCell *cell = nil;
        switch (tableView.tag)
        {
            case kTagBaseTableView:
            {
                if (indexPath.row == 0)
                {
                    static NSString *simpleTableIdentifier = @"Cell";

                    cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

                    if (cell == nil) {
                        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
                    }
                    cell.backgroundColor = [UIColor purpleColor];
                    //return cell;
                }
                if (indexPath.row == 1)
                {
                    static NSString *simpleTableIdentifier = @"Cell1";

                    cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

                    if (cell == nil) {
                        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
                    }
                    self.scrollView1 = (UIScrollView *)[cell viewWithTag:6];
                    self.customStoryboardPageControl = (TAPageControl *)[cell viewWithTag:7];

                    self.scrollView1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, cell.frame.size.height+55)];
                    [self.scrollView1 setContentOffset:CGPointZero animated:NO];

                    self.scrollView1.delegate = self;

                    self.customStoryboardPageControl = [[TAPageControl alloc] init];
                    self.customStoryboardPageControl.frame = CGRectMake(10, 80, tableView.frame.size.width, 30);

                    self.customStoryboardPageControl.numberOfPages = self.imagesData.count;

                    [[cell contentView] addSubview:self.scrollView1];
                    [[cell contentView] addSubview:self.customStoryboardPageControl];

                    [self setupScrollViewImages];

                    cell.backgroundColor = [UIColor yellowColor];
                    //return cell;
                }
            }
            case kTagInnerTableView:
            {
                ORGContainerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ORGContainerCell"];
                cell.backgroundColor = [UIColor greenColor];
                [cell setCollectionData:self.sampleData];
                [cell setCollectionImage:self.sampleImage];
                cell.textLabel.text = @"Horizontal TableView";
                cell.backgroundColor = [UIColor redColor];
                //return cell;
            }
            default:
                break;
        }
        return cell;
    }

【问题讨论】:

  • 你检查过你的表格标签吗?如果在这两种情况下都调用它。
  • 是的。我仔细检查了我的表格标签。

标签: ios objective-c uitableview storyboard


【解决方案1】:

你应该使用cell =而不是ORGContainerCell *cell =

【讨论】:

  • Nick Answer 解决了我的崩溃问题但是我的 Inner 表没有加载。
  • 更好的是,将其重命名为ORGContainerCell *containerCell(因为需要对其调用某些方法),然后使用cell = containerCell;
  • @MihirOza 你为什么使用两个表格视图?
  • 在子表视图中我添加了第三方水平滚动视图。
【解决方案2】:

而不是UITableViewCell *cell = nil;

试试UITableViewCell *cell = [[UITableViewCell alloc] init];

【讨论】:

  • 谢谢。解决我的崩溃问题。但我的内表没有加载。帮我解决这个问题。
  • 对于该问题,请尝试@Bannings 解决方案。
  • 我要去掉 * 号吗?
  • 它不起作用。因为 ORGContainerCell 是第三方单元。
  • ORGContainerCell 未初始化返回 nil。
【解决方案3】:

错误消息已经告诉您:有时,您没有返回 UITableViewCell 对象。

添加这样的保护措施:

return cell ?: [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:nil];

(非标准 ?: 运算符返回左侧,如果它是“真”[非零],否则返回右侧)

【讨论】:

    【解决方案4】:

    您在父表视图中有 3 行,但您只为其中两个创建了单元格。您应该在第一个案例块中为第三个条件编写创建单元格代码

    else if (indexPath.row == 2)
    

    并在其中创建您的第二个表格视图。

    【讨论】:

      【解决方案5】:

      已编辑:

      在您的情况kTagInnerTableView 中,您已经获取了ORGContainerCell 的新对象,因此您可以从switch-casecase 中返回ORGContainerCell 的本地对象。

      只需使用最初声明的对象,如

      case kTagInnerTableView:
                  {
                  ORGContainerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ORGContainerCell"];
                  cell.backgroundColor = [UIColor greenColor];
                  [cell setCollectionData:self.sampleData];
                  [cell setCollectionImage:self.sampleImage];
                  cell.textLabel.text = @"Horizontal TableView";
                  cell.backgroundColor = [UIColor redColor];
                  return cell;                
                  }
      

      【讨论】:

      • 不,我不能这样添加。因为collectiondata 和collectionimage 方法不使用此代码调用。
      • 如果我取消注释返回单元格。我的应用崩溃了。
      • 对不起,我不明白你的回答。请您解释一下。
      【解决方案6】:

      这是最终的工作代码。

          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *CellIdentifier1 = @"Cell";
          static NSString *CellIdentifier2 = @"Cell1";
          static NSString *ORGContainerCellID1 = @"ORGContainerCell1";
          static NSString *ORGContainerCellID2 = @"ORGContainerCell2";
          static NSString *ORGContainerCellID3 = @"ORGContainerCell3";
      
          if (indexPath.section ==0)
          {
              UITableViewCell *cell; //= [[UITableViewCell alloc] init];
              if(indexPath.row == 0)
              {
                  cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
      
                  if (cell == nil) {
                      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
                  }
                  btnSignIn = (UIButton *)[cell viewWithTag:2];
                  lblUserName = (UILabel *)[cell viewWithTag:999];
      
                  NSUserDefaults *defaults= [NSUserDefaults standardUserDefaults];
                  if ([defaults boolForKey:@"hasUserLoggedIn"])
                  {
                      btnSignIn.hidden = TRUE;
                      lblUserName.hidden = FALSE;
                      lblUserName.text = [NSString stringWithFormat:@"Hello,%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"userName"]];
                  }
                  else
                  {
                      btnSignIn.hidden = FALSE;
                      lblUserName.hidden = TRUE;
                  }
                  [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
                  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                  return cell;
              }
              else
              {
                   cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
      
                  if (cell == nil) {
                      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
                  }
                  cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
                  self.scrollView1 = (UIScrollView *)[cell viewWithTag:6];
                  self.customStoryboardPageControl = (TAPageControl *)[cell viewWithTag:7];
                  self.scrollView1.delegate = self;
      
      
                  self.customStoryboardPageControl.numberOfPages = arrBannerData.count;
      
                  [[cell contentView] addSubview:self.scrollView1];
      
                  [self setupScrollViewImages];
                  [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
              }
              return cell;
          }
          else
          {
              if(indexPath.row == 0)
              {
                  ORGContainerCell *cell = [[ORGContainerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ORGContainerCellID1];
                  if (cell == nil)
                  {
                      cell = [[ORGContainerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ORGContainerCellID1] ;
                  }
                  [cell setCollectionData1:self.arrProductName];
                  [cell setCollectionImage1:self.arrProductImage];
                  [cell setCollectionPrice1:arrProductPrice];
                  [cell setCollectionHeader1:arrHeaderTitle];
      
                  return  cell;
              }
              else if(indexPath.row == 1)
              {
                  ORGContainerCell *cell = [[ORGContainerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ORGContainerCellID2];
                  if (cell == nil)
                  {
                      cell = [[ORGContainerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ORGContainerCellID2] ;
                  }
                  [cell setCollectionData1:self.arrProductName1];
                  [cell setCollectionImage1:self.arrProductImage1];
                  [cell setCollectionPrice1:self.arrProductPrice1];
                  [cell setCollectionHeader1:self.arrHeaderTitl1];
      
                  return  cell;
              }
              else
              {
                  ORGContainerCell *cell = [[ORGContainerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ORGContainerCellID3];
                  if (cell == nil)
                  {
                      cell = [[ORGContainerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ORGContainerCellID3] ;
                  }
                  [cell setCollectionData1:self.arrProductName2];
                  [cell setCollectionImage1:self.arrProductImage2];
                  [cell setCollectionPrice1:self.arrProductPrice2];
                  [cell setCollectionHeader1:self.arrHeaderTitl2];
                  return  cell;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-10-09
        • 2013-09-24
        • 2012-03-19
        • 1970-01-01
        • 1970-01-01
        • 2011-08-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多