Ios 多个UITableViewCells,每行有不同的字段

Ios 多个UITableViewCells,每行有不同的字段,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有两个表视图,第一个仅从数组加载列表。 另一个1,显示每行的详细信息。但它显示了一个崩溃报告: 'UITableView数据源必须从tableView:cellForRowAtIndexPath:Exception返回单元格' 我的代码有什么问题?我想向每一行显示来自同一sqlite行的不同细节 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat

我有两个表视图,第一个仅从数组加载列表。 另一个1,显示每行的详细信息。但它显示了一个崩溃报告:

'UITableView数据源必须从tableView:cellForRowAtIndexPath:Exception返回单元格'

我的代码有什么问题?我想向每一行显示来自同一sqlite行的不同细节

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     UITableViewCell *cell;
     //tableView 1  
     if(tableView == self.cTableLabel)
     {
          cell = [tableView dequeueReusableCellWithIdentifier:@"courseCell"];
          NSLog(@"Here.");
          Course *courses = [self.course objectAtIndex:indexPath.row];
          cell.textLabel.text =courses.cName;
          cell.detailTextLabel.text =courses.cSchool;
          return cell;
     }
     //tableView 2
     if(tableView == self.jTableLabel)
     {
          if (indexPath.row == 0)
          {
               cell = [tableView dequeueReusableCellWithIdentifier:@"JobName"];
               cell.textLabel.text = _jDetails.jName;
          }
          else if (indexPath.row == 1)
          {
               cell=  [tableView dequeueReusableCellWithIdentifier:@"JobEarnings"];
               cell.textLabel.text  = @"Job Earnings (per month): Php";
               cell.detailTextLabel.text = _jDetails.jEarnings;
          }
          return cell;
     }
     return 0;

}
首先,检查numberOfRowsInSection是否返回表的正确行数。我想就是这样

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableView == self.cTableLabel)
    {
        return [self.course count];

    }
    //tableView 2
    if(tableView == self.jTableLabel)
    {
        return 2;
    }

    return 0;
}
否则,我将在返回0时设置断点;或者在cellForRowAtIndexPath中,在它前面加上NSLog,因为我的猜测是,如果块被命中并且返回零,则两者都不是。它被触发了吗

编辑-啊,我想我看到dequeueReusableCellWithIdentifier:将返回一个nil单元格,如果您还没有设置。尝试改用dequeueReusableCellWithIdentifier:forIndexPath:。假设您调用了registerClass:[UITableViewCell类]forCellReuseIdentifier:首先,这将始终返回一个有效的单元格

试一试:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"courseCell"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"JobName"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"JobEarnings"];

    //tableView 1
    if(tableView == self.cTableLabel)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"courseCell" forIndexPath:indexPath];
        NSLog(@"Here.");
        Course *courses = [self.course objectAtIndex:indexPath.row];
        cell.textLabel.text =courses.cName;
        cell.detailTextLabel.text =courses.cSchool;
        return cell;
    }
    //tableView 2
    if(tableView == self.jTableLabel)
    {
        if (indexPath.row == 0)
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"JobName" forIndexPath:indexPath];
            cell.textLabel.text = _jDetails.jName;
        }
        else if (indexPath.row == 1)
        {
            cell=  [tableView dequeueReusableCellWithIdentifier:@"JobEarnings" forIndexPath:indexPath];
            cell.textLabel.text  = @"Job Earnings (per month): Php";
            cell.detailTextLabel.text = _jDetails.jEarnings;
        }
        return cell;
    }
    return 0;

}

此外,registerClass:[UITableViewCell类]forCellReuseIdentifier:可以在init中调用,只需声明一次。

出于性能原因,当表视图的数据源将单元格分配给其tableView:CellForRowatineXpath:方法中的行时,通常应重用UITableViewCell对象。表视图维护数据源标记为可重用的UITableViewCell对象的队列或列表。当要求为表视图提供新单元格时,从数据源对象调用此方法。如果现有单元格可用,此方法将其出列,或者使用以前注册的类或nib文件创建新单元格。如果没有可重用的单元格,并且您没有注册类或nib文件,则此方法返回nil


如果为指定的标识符注册了一个类,并且必须创建一个新的单元格,则此方法通过调用其initWithStyle:reuseIdentifier:method来初始化单元格。对于基于nib的单元,此方法从提供的nib文件加载单元对象。如果现有单元格可供重用,此方法将调用单元格的prepareForReuse方法。

如果需要为每个tableview创建不同的单元格,请尝试使用上述代码:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell;
         //tableView 1  
         if(tableView == self.cTableLabel)
         {
     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        else
        {
            for(UIView *view in cell.contentView.subviews)
            {
                [view removeFromSuperview];
            }
        }
              NSLog(@"Here.");
              Course *courses = [self.course objectAtIndex:indexPath.row];
              cell.textLabel.text =courses.cName;
              cell.detailTextLabel.text =courses.cSchool;
              return cell;
         }
         //tableView 2
         if(tableView == self.jTableLabel)
         {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        else
        {
            for(UIView *view in cell.contentView.subviews)
            {
                [view removeFromSuperview];
            }
        }
              if (indexPath.row == 0)
              {
                   cell = [tableView dequeueReusableCellWithIdentifier:@"JobName"];
                   cell.textLabel.text = _jDetails.jName;
              }
              else if (indexPath.row == 1)
              {
                   cell=  [tableView dequeueReusableCellWithIdentifier:@"JobEarnings"];
                   cell.textLabel.text  = @"Job Earnings (per month): Php";
                   cell.detailTextLabel.text = _jDetails.jEarnings;
              }
              return cell;
         }
         return 0;

    }

请在numberOfRowsInSection方法中交叉检查您的表中有一个返回的行数更多。可能是您没有检查此方法,因为两个表返回的行数不同。

Hmm。。那么它命中了哪个返回语句呢?