Mazen
Mazen

Reputation: 45

How do I show multiple custom cells using objective C?

I have a few custom cells that I would like to display information on. I made 2 new cells however it is only returning one cell. The top most cell's identifier is "Cell" and the bottom one is "quantityCell". I would like the product description to be on the top cell and the quantity on the bottom cell. The output I am getting now is only the quantity cell showing, but not the product cell. If I remove the quantity cell, the product cell will show. How should I go about doing this? Thank you

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

Upvotes: 1

Views: 2394

Answers (3)

E-Riddie
E-Riddie

Reputation: 14780

Problem

The you can't return 2 parameters in the same time on a method. So where you say:

return cell;return quantityCell;

Only cell is being returned, the method ends on the first return.

Solution

You need to make logic which one time returns topCell and the other time bottomCell

So in your case you just need to have indexPath.row modulus 2, which can take any value and returns only 0 on even numbers and 1 on odd numbers.

So your code should be like this:

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

Upvotes: 2

Mazen
Mazen

Reputation: 45

Thank you all, I ended up using a dictionary that returns all of the keys and values to the cells.

    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;

Upvotes: 0

Rashad
Rashad

Reputation: 11197

return cell;
return quantityCell;

The above 2 line you wrote in your function! After return statement the method stop executing further lines.

You could do something like this:

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

Hope this helps.. :)

Upvotes: 0

Related Questions