2011-05-22 91 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

相关问题