2012-04-27 32 views
0

我有一个UITableView,用于尝试显示2个不同的对象 - 收据和milages。iPhone - 尝试在表格视图中显示2种不同的对象类型

这是我用尝试完成原代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    Project *project = [[Project alloc] init]; 
    project = [appDelegate.projects objectAtIndex:indexPath.section]; 

    if (indexPath.row >= [project.receipts count]) 
    { 
     if (indexPath.row >= [project.milages count]){ 
      return NULL; 
     } 
     else { 
      //Create a new journey cell and set its UI values 
      MilageCell *cell = (MilageCell *)[tableView 
               dequeueReusableCellWithIdentifier:@"MilageCell"]; 
      Milage *milage = [project.milages objectAtIndex:indexPath.row]; 
      cell.locationLabel.text = milage.location; 
      cell.dateLabel.text = [NSString stringWithFormat:@"%@", milage.date]; 
      cell.totalLabel.text = [NSString stringWithFormat:@"£%@", milage.total]; 
      return cell; 
     } 
    } 
    else 
    { 
     ReceiptCell *cell = (ReceiptCell *)[tableView 
              dequeueReusableCellWithIdentifier:@"ReceiptCell"]; 
     Receipt *receipt = [project.receipts objectAtIndex:indexPath.row]; 
     cell.dateLabel.text = receipt.receiptDate; 
     cell.descriptionLabel.text = receipt.descriptionNote; 
     cell.amountLabel.text = [NSString stringWithFormat:@"£%@", receipt.amount]; 
     return cell; 
    } 

} 

我知道,如果我在每个阵列1个对象(project.receipts和project.milages)然后这段代码根本不起作用。我也知道你不能从这个方法返回NULL。我试图做的是以某种方式显示我的所有收据对象,然后显示所有我的微软对象(表格视图中的部分0有3个收据和3个微缩,它将首先显示收据,然后显示收集)。然而,我完全不知道如何做到这一点,有人可以解释我可以如何解决这个问题?有什么办法可以构建一个所有的微缩和接收对象的单个数组,然后以某种方式辨别哪种对象具有此方法以使用适当的单元格类型?

非常感谢,

杰克

+0

为什么你首先'init'你的项目,然后覆盖它?首先它没有任何意义,其次,你正在泄漏内存:'Project * project = [[Project alloc] init]; project = [appDelegate.projects objectAtIndex:indexPath.section];' – 2012-04-27 17:12:20

回答

1

试试这个,我刚刚删除的,如果条件并从index.row的收据细胞减去[project.milages count]

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    Project *project = [[Project alloc] init]; 
    project = [appDelegate.projects objectAtIndex:indexPath.section]; 

     if (indexPath.row >= [project.milages count]){ 
      ReceiptCell *cell = (ReceiptCell *)[tableView 
               dequeueReusableCellWithIdentifier:@"ReceiptCell"]; 
      Receipt *receipt = [project.receipts objectAtIndex:indexPath.row-[project.milages count]]; 
      cell.dateLabel.text = receipt.receiptDate; 
      cell.descriptionLabel.text = receipt.descriptionNote; 
      cell.amountLabel.text = [NSString stringWithFormat:@"£%@", receipt.amount]; 
      return cell; 
     } 
     else { 
      //Create a new journey cell and set its UI values 
      MilageCell *cell = (MilageCell *)[tableView 
               dequeueReusableCellWithIdentifier:@"MilageCell"]; 
      Milage *milage = [project.milages objectAtIndex:indexPath.row]; 
      cell.locationLabel.text = milage.location; 
      cell.dateLabel.text = [NSString stringWithFormat:@"%@", milage.date]; 
      cell.totalLabel.text = [NSString stringWithFormat:@"£%@", milage.total]; 
      return cell; 
     } 
} 

您还可以使用两个不同部分的UITableView

相关问题