2014-01-20 18 views
0

我有2個tableViews,第一個只從數組加載列表。 其他1,每行顯示詳細信息。但它顯示了一個崩潰報告:多個UITableViewCells每行不同的字段

「的UITableView數據源必須的tableView返回細胞:的cellForRowAtIndexPath:異常」

出了什麼錯我的代碼?我想要顯示每行來自同一個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; 

} 

回答

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

否則我將在return 0;或NSLog的設置斷點之前在cellForRowAtIndexPath,因爲我的猜測是沒有,如果塊擊中,你是返回零。它會被觸發嗎?

編輯 - 啊,我想我看到dequeueReusableCellWithIdentifier:將返回一個零單元格,如果你還沒有設置一個呢。改用dequeueReusableCellWithIdentifier: forIndexPath:來代替。這將始終返回一個有效的單元格,假設您首先調用registerClass:[UITableViewCell class] 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 class] forCellReuseIdentifier:可以在你的init被調用,只需要聲明一次。

+0

它沒有命中斷點。 :\ n也沒有NSLog – hazelvan

+0

嗯..那麼哪個返回語句碰到了? – ansible

0

出於性能方面的原因,表格視圖的數據源在將單元格分配給其tableView:cellForRowAtIndexPath:方法中的行時通常應該重用UITableViewCell對象。表視圖維護數據源標記爲可重用的UITableViewCell對象的隊列或列表。當被要求提供表格視圖的新單元格時,從數據源對象調用此方法。此方法將取出現有單元格(如果有)或使用之前註冊的類或nib文件創建新單元格。如果沒有單元可用於重用並且未註冊類或nib文件,則此方法返回nil。

如果您爲指定的標識符註冊了一個類並且必須創建一個新的單元格,則此方法通過調用其方法initWithStyle:reuseIdentifier:來初始化該單元格。對於基於筆尖的單元格,此方法從提供的nib文件加載單元格對象。如果現有單元可供重用,則此方法將調用單元的prepareForReuse方法。

0

你需要爲每個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; 

    } 
0

請交叉檢查或者您的表中的一個在numberOfRowsInSection方法返回更多的行數。這可能是因爲兩個表都返回不同的行數,所以您沒有檢查此方法。

相關問題