2011-05-22 85 views
3

嘿,大家好,我很困惑如何在一個UITableView中使用兩個不同的單元格類型和兩個部分。第一節應該返回一個大的單元格,其中有很多文本,另一節應該返回三個單元格,以導航到其他視圖。我想這樣的:兩個不同的單元格類型在一個UITableView中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //NSUInteger section; 
    static NSString *CellIdentifier1 = @"CellIdentifier"; 
    static NSString *CellIdentifier2 = @"Cell"; 

    if (indexPath.section == 0) { 

     CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
     if (cell == nil) 
     { 
      [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil];  cell = [self customTableCell]; 
      [self setCustomTableCell:nil]; 
     } 
     return cell; 
    } 
    else if (indexPath.section == 1) { 

      cTableViewCell *cell = (cTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 
      if (cell == nil) 
      { 
       [[NSBundle mainBundle] loadNibNamed:@"cTableViewCell" owner:self options:nil]; 
       cell = [[cTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2]; 
       [self setCustomTableCell:nil]; 
      } 
      // Configure the cell... 
      NSUInteger row = [indexPath row]; 
      cell.textLabel.text = [array objectAtIndex:row]; 
      return cell;    
     } 
    return cell; 
} 

我設定的高度在這裏的部分細胞:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger section; 
    if (section == 0) { 
     return [indexPath row] + 80; 
    } 
    else 
    { 
     return [indexPath row] + 44; 
    } 

} 

我得到這個錯誤:「細胞」未申報(第一次使用此功能)。 :(我真的希望你能幫助我。提前

+0

你從哪裏得到這個錯誤嗎? – pmerino 2011-05-22 18:28:32

回答

4

截至tableView:cellForRowAtIndexPath:年底謝謝,使其return nil;,而不是return cell;

+6

這將是更好申報'細胞'if-else語句之外,例如'UITableViewCell * cell;',並且從if-else語句中刪除'return cell'語句,並且在方法結尾處僅返回'return cell'。如果'tableView: cellForRowAtIndexPath:'永遠返回'nil',UIKit拋出異常 – ma11hew28 2011-07-26 22:35:20

+0

@MattDiPasquale在我的tableview數據滾動時越來越亂,但是當我設置dequereuable功能爲零然後它的作品,堅果數據是動態的,所以我不想使用零,有沒有其他辦法,請幫助男人。 – 2013-04-04 05:40:09

相關問題