2012-04-27 13 views
0

私はUITableViewを使用して、2つの異なるオブジェクト(領収書と金額)を表示しようとしています。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; 
    } 

} 

私はアレイ(project.receiptsとproject.milages)のそれぞれに1つのオブジェクトを持っている場合ことを知ってるそして、このコードはまったく動作しません。このメソッドからNULLを返すことはできません。私がしようとしていることは何とかすべてのレシートオブジェクトを表示してから、すべてのマイレージオブジェクトを表示します(テーブルビューのセクション0は3つの領収書と3つの金額を持っています。しかし、私はこれをどうやって行うのか全く分かりません。誰かがこの問題をどのように解決できるか説明できますか?どのような方法で私はすべてのmilageと領収書のオブジェクトの単一の配列を構築し、何らかの形で私はこのメソッドでは、適切な細胞型を使用するために持っているオブジェクトの種類を識別することができますか?

どうもありがとう、

ジャック

+0

なぜあなたはまずあなたのプロジェクトを 'init'して上書きしますか?まず第一にそれは意味をなさない。そして第二に、あなたは記憶を漏らしている: 'Project * project = [[Project alloc] init]; project = [appDelegate.projects objectAtIndex:indexPath.section]; ' –

答えて

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を2つの異なるセクションで使用することもできます。

関連する問題