如何使用目标C显示多个自定义单元格?

时间:2015-01-28 08:23:27

标签: ios objective-c uitableview

我想要显示一些自定义单元格。我制作了2个新细胞,但它只返回了一个细胞。最顶层的小区标识符是" Cell"而最底层的是" quantityCell"。我希望产品描述在顶部单元格和底部单元格上的数量。我现在得到的输出只是数量单元格显示,而不是产品单元格。如果我删除数量单元格,将显示产品单元格。我该怎么做呢?  谢谢

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSString *quantityCellIdentifier = @"quantityCell";
    UITableViewCell *quantityCell = [tableView dequeueReusableCellWithIdentifier:quantityCellIdentifier forIndexPath:indexPath];

    DisplayInventoryTableViewController *inventoryItem = [[DisplayInventoryTableViewController alloc]init];

    self.productName = [inventoryItem getProductTitle];
    productTitle = self.productName;
    cell.detailTextLabel.text = productTitle;
        NSLog(@"productTitle %@", self.productName);
    cell.textLabel.text = @"Product Title";

    self.productQuantity = [inventoryItem getQuantity];
    quantityCell.textLabel.text = @"Quantity";
    quantityCell.detailTextLabel.text = self.productQuantity;

    /*
    // Configure the cell...

   // cell.textLabel.text = productTitle;
    cell.textLabel.text = @"Product Name";
    cell.detailTextLabel.text = productTitle;
    */
    return cell;return quantityCell;
}

3 个答案:

答案 0 :(得分:2)

问题

您无法在方法上同时返回2个参数。所以在哪里说:

return cell;return quantityCell;

仅返回单元格,方法在第一次返回时结束。

解决方案

您需要制作一次返回 topCell 的逻辑,另一次 bottomCell

所以在你的情况下你只需要indexPath.row 模数 2,它可以取任何值,偶数时只返0,奇数时只返1。

所以你的代码应该是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"";

    if (indexPath.row%2==0) {
        //Get the top cell
        CellIdentifier = @"Cell";
    } else {
        //Get bottom cell
        CellIdentifier = @"quantityCell";
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    DisplayInventoryTableViewController *inventoryItem = [[DisplayInventoryTableViewController alloc]init];
    if (indexPath.row%2==0) {
        //Customize topCell
        self.productName = [inventoryItem getProductTitle];
        productTitle = self.productName;
        cell.detailTextLabel.text = productTitle;
        NSLog(@"productTitle %@", self.productName);
        cell.textLabel.text = @"Product Title";
    } else {
        //Customize bottom cell
        self.productQuantity = [inventoryItem getQuantity];
        cell.textLabel.text = @"Quantity";
        cell.detailTextLabel.text = self.productQuantity;
    }

    return cell;
}

答案 1 :(得分:0)

return cell;
return quantityCell;

您在函数中编写的上述2行!在return语句之后,该方法停止执行更多行。

你可以这样做:

self.productName = [inventoryItem getProductTitle];
self.productQuantity = [inventoryItem getQuantity];
productTitle = self.productName;
if(indexPath.row == 0) {
    NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.detailTextLabel.text = productTitle;
    cell.textLabel.text = @"Product Title";
    return cell;
}
else {
    NSString *quantityCellIdentifier = @"quantityCell";
    UITableViewCell *quantityCell = [tableView dequeueReusableCellWithIdentifier:quantityCellIdentifier forIndexPath:indexPath];
    quantityCell.textLabel.text = @"Quantity";
    quantityCell.detailTextLabel.text = self.productQuantity;
    return quantityCell;
}

希望这会有所帮助.. :)

答案 2 :(得分:0)

谢谢大家,我最终使用了一个字典,它将所有键和值都返回给单元格。

    NSDictionary *inventoryDictionary = @{
                                          
                                          @"Quantity"      : productQuantity,
                                          @"Price"         : productPriceValue,
                                          @"Product Title" : productTitle
                                          
                                          };

        NSLog(@"ProductStrings: %@", inventoryDictionary);

    NSString *keys = [inventoryDictionary allKeys][indexPath.row];
    NSString *providerNameString = keys;
    cell.textLabel.text = providerNameString;
    
        NSString *Values = [inventoryDictionary allValues][indexPath.row];
    NSString *providerNameValue = Values;
    cell.detailTextLabel.text = providerNameValue;
    
    return cell;