0

我有一个 UITableView,我用它来尝试显示 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 个里程数,它会首先显示收据,然后是里程数)。但是我完全不知道该怎么做,有人可以解释我如何解决这个问题吗?有没有什么方法可以构造一个包含所有里程和收据对象的数组,然后以某种方式辨别我在这个方法中拥有什么样的对象,以便使用适当的单元格类型?

非常感谢,

杰克

4

1 回答 1

1

试试这个,我刚刚删除了一个 if 条件并[project.milages count]index.rowReceipt 单元格中减去了。

- (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;
        }
}

您还可以将 anUITableView与两个不同的部分一起使用。

于 2012-04-27T16:52:41.443 回答