我有一个UITableView,我正在尝试显示2个不同的对象 - 收据和milages。
这是我用来尝试完成此操作的原始代码:
- (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),那么这段代码根本不起作用。我也知道你不能从这个方法返回NULL。我想要做的是以某种方式显示我的所有收据对象,然后是我所有的milage对象(所以说表视图中的第0部分有3个收据和3个milages,它首先显示收据,然后是milages)。但是我完全不知道怎么做,有人可以解释我怎么解决这个问题吗?有什么方法可以构建所有milage和接收对象的单个数组,然后以某种方式辨别我在这个方法中使用哪种对象以使用适当的单元格类型?
非常感谢,
杰克
答案 0 :(得分:1)
试试这个,我刚删除了if条件,并从[project.milages count]
中减去了index.row
收据单元格。
- (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
两个不同的部分。