私は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と領収書のオブジェクトの単一の配列を構築し、何らかの形で私はこのメソッドでは、適切な細胞型を使用するために持っているオブジェクトの種類を識別することができますか?
どうもありがとう、
ジャック
なぜあなたはまずあなたのプロジェクトを 'init'して上書きしますか?まず第一にそれは意味をなさない。そして第二に、あなたは記憶を漏らしている: 'Project * project = [[Project alloc] init]; project = [appDelegate.projects objectAtIndex:indexPath.section]; ' –